89 Commits

Author SHA1 Message Date
wash fa9572d33a fx: iterator: return NULL value if iterator status is not SUCCESS 2026-05-30 20:19:47 +01:00
wash 8c0a31e19b meta: update rosetta support 2026-05-30 10:09:55 +01:00
wash a20306ae64 cmake: update package finder module 2026-05-29 20:45:46 +01:00
wash 99380c7810 test: fix linux compilation 2026-05-29 20:20:31 +01:00
wash 2d396ee071 fx.reflection: fix linux compilation 2026-05-29 20:20:19 +01:00
wash 34a8ccff95 fx: fix linux compilation 2026-05-29 20:20:05 +01:00
wash d9835cea0b fx.io: fix linux compilation 2026-05-29 20:19:55 +01:00
wash 7b546f6bbe meta: update clang-format config 2026-05-29 20:04:34 +01:00
wash 65f490b900 fx.serial: toml: update array and hashtable usage 2026-05-29 20:04:15 +01:00
wash 1516eee8d1 fx.collections: hashtable: update allocate_index_for_key to handle exact key matches 2026-05-29 20:02:48 +01:00
wash 53a856794f fx.collections: move fx_datetime to fx 2026-05-29 20:02:02 +01:00
wash f879d4fdfc fx: object: add nullptr check to fx_object_is_type 2026-05-29 19:59:55 +01:00
wash e53027595e fx: double add missing fx.comparable reference 2026-05-29 19:59:30 +01:00
wash bbb26c5ee5 fx: pointer: implement hash() and compare() 2026-05-29 19:59:00 +01:00
wash e78241c051 fx: printf: adjust default floating point precision 2026-05-29 19:58:40 +01:00
wash 5877abd33e fx.reflection: update iterator semantics 2026-05-28 21:00:23 +01:00
wash b31e82aaf8 fx.io: update iterator semantics 2026-05-28 20:53:53 +01:00
wash 4c06ded99f fx.cmdline: fix namespace not having the required header subdirectory 2026-05-28 20:53:16 +01:00
wash ff0d40b324 fx.collections: update iterator semantics 2026-05-28 20:52:40 +01:00
wash 883b0b24b1 fx: improve fx_value reference management 2026-05-28 20:51:55 +01:00
wash 516f5be2d6 fx: implement fx_namemap cleanup 2026-05-28 20:50:27 +01:00
wash f79650698a fx: update fx_iterator to return pointers to values 2026-05-28 20:50:00 +01:00
wash 01cbcf712a test: update tests 2026-05-25 17:35:09 +01:00
wash 5e3439bc4a cmake: update handling of public namespace headers 2026-05-25 17:34:59 +01:00
wash 56e1fae273 cmake: update endianness test 2026-05-25 17:34:41 +01:00
wash c5eac75a0b fx.term: remove redundant printf implementation 2026-05-25 17:31:40 +01:00
wash aea3f99859 fx.collections: update exported classes 2026-05-25 17:31:17 +01:00
wash cca52c788a fx.serial: remove references to obsolete classes 2026-05-25 17:30:59 +01:00
wash 74fbd94f95 fx.io: update interface implementations 2026-05-25 17:30:33 +01:00
wash 97fe00d4a1 fx.collections: update interface implementations 2026-05-25 17:30:08 +01:00
wash 0be82c63fa fx.collections: update tests 2026-05-25 17:29:56 +01:00
wash 2df76a76c4 fx.collections: remove fx_tree 2026-05-25 17:29:38 +01:00
wash 9d4aab49ac fx.collections: update fx_array to support storing fx_values 2026-05-25 17:29:23 +01:00
wash d84d56681f fx.collections: replace fx_dict with fx_hashtable 2026-05-25 17:28:25 +01:00
wash 6efe473c11 fx: implement updated interfaces in builtin classes 2026-05-25 17:27:14 +01:00
wash 5d0aa577d6 fx.reflection: fix fx_string type marshalling 2026-05-25 17:26:55 +01:00
wash f9b456a99f fx.reflection: implement assembly registration and lookup 2026-05-25 17:26:08 +01:00
wash 673bf07cf6 fx.reflection: implement retrieving functions and properties from fx_type 2026-05-25 17:25:27 +01:00
wash 82bb20c6af fx.reflection: implement fx_property 2026-05-25 17:24:47 +01:00
wash 12b04eea8d fx: namemap: fix nullptr deference in fx_namemap_first 2026-05-25 17:23:50 +01:00
wash 0f1dfc0b07 fx: implement wchar and wstr value types 2026-05-25 17:23:32 +01:00
wash 13332145c2 fx: add functions for hashing wstr and generic buffers 2026-05-25 17:23:19 +01:00
wash 7cdad180da fx: add interface for performing arithmetic operations on values 2026-05-25 17:21:27 +01:00
wash 554a30fa1a fx: add interface for converting a value to a different type 2026-05-25 17:21:08 +01:00
wash 5058bb164a fx: add interface for comparaing two values 2026-05-25 17:20:36 +01:00
wash 79a8041aac fx: add wstr and wchar value types 2026-05-25 17:20:15 +01:00
wash bc902047d3 fx: implement value conversion interface 2026-05-25 17:19:51 +01:00
wash 61182e0153 fx.reflection: function: update invocation to support new value-types 2026-05-17 17:11:02 +01:00
wash f1258489d1 fx: implement a proper value-type mechanism 2026-05-17 17:09:05 +01:00
wash 65a7a025c5 fx: stream: add fx_stream_write_wstr 2026-05-17 17:08:42 +01:00
wash 5ead9118b3 build: add module to handle querying platform details 2026-05-05 21:31:12 +01:00
wash c29465a97d build: re-enable namespace-specific tests 2026-05-05 21:17:21 +01:00
wash 4b20f8ca0e test: dynamic function call tests 2026-05-05 21:16:56 +01:00
wash f3062222cb fx: support for registering callable functions with types 2026-05-05 21:16:31 +01:00
wash 703155affe meta: update clang-format config 2026-05-05 19:03:31 +01:00
wash 90a3905f48 fx: re-arrange type system to allow for reflection 2026-05-05 19:03:22 +01:00
wash 8c12868651 fx: add simple cstr-based hashmap data structure 2026-05-05 12:38:34 +01:00
wash 2966934cd8 fx: add simple resizable array data structure 2026-05-05 12:38:12 +01:00
wash 9937546452 fx.reflection: remove fx_function_context 2026-05-04 22:56:21 +01:00
wash 5d98bc89a1 test: dynamic function argument bind test 2026-05-04 22:54:02 +01:00
wash b8e270b962 fx.reflection: function: implementing pre-binding argument values to functions 2026-05-04 22:53:45 +01:00
wash 3d1600ece6 fx: value: implement copy and unset functions 2026-05-04 22:53:23 +01:00
wash fe3af271e2 meta: update clang-format config 2026-05-04 22:53:03 +01:00
wash dea321acc2 fx.reflection: optimise darwin dynamic function calling 2026-05-04 20:44:02 +01:00
wash abe1603955 meta: update clang-format config 2026-05-04 19:42:37 +01:00
wash bf251dd818 fx.reflection: optimise linux dynamic function calling 2026-05-04 19:37:44 +01:00
wash bebc67537d fx.reflection: implement dynamic function calling on linux-x86_64 2026-05-04 19:23:10 +01:00
wash e954a15d92 fx.reflection: cleanup darwin implementation of dynamic function calling 2026-05-04 19:22:41 +01:00
wash 18c9d30c60 meta: re-organise tests 2026-05-04 16:37:06 +01:00
wash 716b939d4f fx.serial: toml: replace fx_number with new number types 2026-05-04 16:36:43 +01:00
wash 5aae7dff6b fx: add macro to initialise an empty fx_value 2026-05-04 16:36:07 +01:00
wash 32264d693c fx: add stub files for enum type registration 2026-05-04 16:34:32 +01:00
wash fd35c2b03e fx.reflection: add stub files for object properties 2026-05-04 16:33:51 +01:00
wash bf7d8cfb75 fx.reflection: implement dynamic function calling 2026-05-04 16:33:16 +01:00
wash 85ff8b7eaa fx.collections: dict: remove duplicate cstr hash function 2026-05-03 20:36:50 +01:00
wash dab3fec6b8 fx: add a generic container for primitive values and objects 2026-05-03 20:22:05 +01:00
wash 7d44a6ec98 fx: split fx_number into dedicated int, uint, double, and bool types 2026-05-03 20:21:02 +01:00
wash 7b5173127c build: remove relative directory paths 2026-05-03 20:20:16 +01:00
wash 6777c09f67 fx.collections: move fx_uuid to fx 2026-05-03 17:18:50 +01:00
wash d75ec65721 fx.collections: move fx_number to fx 2026-05-03 17:18:39 +01:00
wash 87270b686c build: re-organise assembly build files 2026-05-03 17:05:48 +01:00
wash f4469c9eb0 fx.cmdline: convert to new assembly build system 2026-05-03 16:49:33 +01:00
wash 44fed67d43 fx.term: convert to new assembly build system 2026-05-03 16:49:23 +01:00
wash 0c53974ac9 fx.serial: convert to new assembly build system 2026-05-03 16:48:42 +01:00
wash 3f7ad3ab08 fx.compression: convert to new assembly build system 2026-05-03 16:29:23 +01:00
wash cefb548824 fx.io: convert to assembly build system 2026-05-03 14:51:11 +01:00
wash 55ba15b1fa fx: fix darwin compilation 2026-05-03 14:51:01 +01:00
wash 90d180d839 meta: update clang-format config 2026-05-03 14:50:50 +01:00
wash a0283da135 build: fix namespace build macro 2026-05-03 14:47:59 +01:00
255 changed files with 14165 additions and 8620 deletions
+9 -125
View File
@@ -1,7 +1,5 @@
BasedOnStyle: WebKit BasedOnStyle: WebKit
IndentWidth: 8 IndentWidth: 8
---
Language: C
DerivePointerAlignment: false DerivePointerAlignment: false
PointerAlignment: Right PointerAlignment: Right
ColumnLimit: 80 ColumnLimit: 80
@@ -27,7 +25,7 @@ AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: true AlwaysBreakBeforeMultilineStrings: true
AlwaysBreakTemplateDeclarations: Yes AlwaysBreakTemplateDeclarations: Yes
BinPackArguments: false BinPackArguments: false
BinPackParameters: OnePerLine BinPackParameters: false
ExperimentalAutoDetectBinPacking: false ExperimentalAutoDetectBinPacking: false
BitFieldColonSpacing: Both BitFieldColonSpacing: Both
BreakBeforeBraces: Linux BreakBeforeBraces: Linux
@@ -46,131 +44,17 @@ ReflowComments: true
SpacesBeforeTrailingComments: 3 SpacesBeforeTrailingComments: 3
TabWidth: 8 TabWidth: 8
UseTab: AlignWithSpaces UseTab: AlignWithSpaces
PenaltyReturnTypeOnItsOwnLine: 1000000 BreakAfterReturnType: Automatic
PenaltyExcessCharacter: 999999999 PenaltyBreakAssignment: 0
PenaltyBreakOpenParenthesis: 5 PenaltyReturnTypeOnItsOwnLine: 100000000
PenaltyBreakBeforeFirstCallParameter: 5 PenaltyExcessCharacter: 10000000
PenaltyBreakOpenParenthesis: 0
PenaltyBreakBeforeFirstCallParameter: 0
PenaltyIndentedWhitespace: 0 PenaltyIndentedWhitespace: 0
AttributeMacros: AttributeMacros:
- FX_API - FX_API
ForEachMacros: ForEachMacros:
- fx_btree_foreach - fx_btree_foreach
- fx_queue_foreach - fx_queue_foreach
MacroBlockBegin: "FX_(TYPE|ASSEMBLY)_.*_BEGIN" MacroBlockBegin: "FX_(TYPE|ASSEMBLY).*_BEGIN"
MacroBlockEnd: "FX_(TYPE|ASSEMBLY)_.*_END" MacroBlockEnd: "FX_(TYPE|ASSEMBLY).*_END"
---
Language: ObjC
DerivePointerAlignment: false
PointerAlignment: Right
ColumnLimit: 80
AlignAfterOpenBracket: AlwaysBreak
AlignConsecutiveAssignments: None
AlignConsecutiveBitFields: None
AlignConsecutiveDeclarations: None
AlignConsecutiveMacros: AcrossEmptyLinesAndComments
AlignEscapedNewlines: Right
AlignOperands: AlignAfterOperator
AlignTrailingComments: true
AllowAllArgumentsOnNextLine: false
AllowAllConstructorInitializersOnNextLine: false
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortBlocksOnASingleLine: Empty
AllowShortCaseLabelsOnASingleLine: false
AllowShortEnumsOnASingleLine: false
AllowShortFunctionsOnASingleLine: false
AllowShortIfStatementsOnASingleLine: false
AllowShortLambdasOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: true
AlwaysBreakTemplateDeclarations: Yes
BinPackArguments: false
BinPackParameters: true
ExperimentalAutoDetectBinPacking: false
BitFieldColonSpacing: Both
BreakBeforeBraces: Linux
BreakBeforeBinaryOperators: All
BreakBeforeTernaryOperators: true
BreakConstructorInitializers: BeforeComma
BreakInheritanceList: BeforeComma
BreakStringLiterals: true
ContinuationIndentWidth: 8
Cpp11BracedListStyle: true
IncludeBlocks: Regroup
SortIncludes: true
IndentRequires: true
NamespaceIndentation: Inner
ReflowComments: true
SpacesBeforeTrailingComments: 3
TabWidth: 8
UseTab: AlignWithSpaces
PenaltyReturnTypeOnItsOwnLine: 1000000
PenaltyExcessCharacter: 5
PenaltyBreakOpenParenthesis: 5
PenaltyBreakBeforeFirstCallParameter: 5
PenaltyIndentedWhitespace: 0
AttributeMacros:
- FX_API
ForEachMacros:
- fx_btree_foreach
- fx_queue_foreach
MacroBlockBegin: "FX_(TYPE|ASSEMBLY)_.*_BEGIN"
MacroBlockEnd: "FX_(TYPE|ASSEMBLY)_.*_END"
---
Language: Cpp
DerivePointerAlignment: false
PointerAlignment: Right
ColumnLimit: 80
AlignAfterOpenBracket: AlwaysBreak
AlignConsecutiveAssignments: None
AlignConsecutiveBitFields: None
AlignConsecutiveDeclarations: None
AlignConsecutiveMacros: AcrossEmptyLinesAndComments
AlignEscapedNewlines: Right
AlignOperands: AlignAfterOperator
AlignTrailingComments: true
AllowAllArgumentsOnNextLine: false
AllowAllConstructorInitializersOnNextLine: false
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortBlocksOnASingleLine: Empty
AllowShortCaseLabelsOnASingleLine: false
AllowShortEnumsOnASingleLine: false
AllowShortFunctionsOnASingleLine: false
AllowShortIfStatementsOnASingleLine: false
AllowShortLambdasOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: true
AlwaysBreakTemplateDeclarations: Yes
BinPackArguments: false
BinPackParameters: OnePerLine
ExperimentalAutoDetectBinPacking: false
BitFieldColonSpacing: Both
BreakBeforeBraces: Linux
BreakBeforeBinaryOperators: All
BreakBeforeTernaryOperators: true
BreakConstructorInitializers: BeforeComma
BreakInheritanceList: BeforeComma
BreakStringLiterals: true
ContinuationIndentWidth: 8
Cpp11BracedListStyle: true
IncludeBlocks: Regroup
SortIncludes: true
IndentRequires: true
NamespaceIndentation: Inner
ReflowComments: true
SpacesBeforeTrailingComments: 3
TabWidth: 8
UseTab: AlignWithSpaces
PenaltyReturnTypeOnItsOwnLine: 1000000
PenaltyExcessCharacter: 9999999
PenaltyBreakOpenParenthesis: 5
PenaltyBreakBeforeFirstCallParameter: 5
PenaltyIndentedWhitespace: 0
AttributeMacros:
- FX_API
ForEachMacros:
- fx_btree_foreach
- fx_queue_foreach
MacroBlockBegin: "FX_(TYPE|ASSEMBLY)_.*_BEGIN"
MacroBlockEnd: "FX_(TYPE|ASSEMBLY)_.*_END"
+32 -22
View File
@@ -1,17 +1,35 @@
cmake_minimum_required(VERSION 3.25) cmake_minimum_required(VERSION 3.25)
project(fx C) project(fx C ASM)
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
include (TestBigEndian) include (TestBigEndian)
include(Templates)
include(Platform)
test_big_endian(is_big_endian)
if (is_big_endian)
message(STATUS "Target is Big Endian")
add_compile_definitions(FX_ENDIAN=1)
else ()
message(STATUS "Target is Little Endian")
add_compile_definitions(FX_ENDIAN=0)
endif ()
set(CMAKE_C_STANDARD 99) set(CMAKE_C_STANDARD 99)
set(CMAKE_C_EXTENSIONS OFF) set(CMAKE_C_EXTENSIONS OFF)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_SOURCE_DIR}/cmake)
set_property(GLOBAL PROPERTY USE_FOLDERS ON) set_property(GLOBAL PROPERTY USE_FOLDERS ON)
set(fx_all_assemblies set(fx_source_root ${CMAKE_CURRENT_SOURCE_DIR})
fx.runtime set(fx_build_root ${CMAKE_CURRENT_BINARY_DIR})
fx.collections) file(GLOB _all_assemblies assemblies/*
LIST_DIRECTORIES TRUE)
foreach (assembly_path ${_all_assemblies})
get_filename_component(assembly_name ${assembly_path} NAME)
set(fx_all_assemblies ${fx_all_assemblies} ${assembly_name})
endforeach (assembly_path)
if (NOT DEFINED fx_assemblies) if (NOT DEFINED fx_assemblies)
set(fx_assemblies ${fx_all_assemblies}) set(fx_assemblies ${fx_all_assemblies})
@@ -27,28 +45,20 @@ endif ()
message(STATUS "Floating point support: ${fx_enable_floating_point}") message(STATUS "Floating point support: ${fx_enable_floating_point}")
set(fx_system_name ${CMAKE_SYSTEM_NAME}) get_platform_details(
string(TOLOWER ${fx_system_name} fx_system_name) SYSTEM fx_system_name
PROCESSOR fx_system_arch)
message(STATUS "System name: ${fx_system_name}") message(STATUS "Target system: ${fx_system_name}-${fx_system_arch}")
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/lib) set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/lib) set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/bin) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/bin)
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/Templates.cmake) foreach (assembly ${fx_assemblies})
add_subdirectory(assemblies/${assembly})
endforeach (assembly)
if ("fx.runtime" IN_LIST fx_assemblies) if (fx_enable_tests)
add_fx_assembly( add_subdirectory(test)
NAME fx.runtime
NAMESPACES fx fx.reflection)
endif () endif ()
if ("fx.collections" IN_LIST fx_assemblies)
add_fx_assembly(
NAME fx.collections
NAMESPACES fx.collections
DEPENDENCIES fx.runtime)
endif ()
add_executable(dynamic-test test/dynamic-test.c)
+7
View File
@@ -0,0 +1,7 @@
add_fx_assembly(
NAME fx.cmdline
NAMESPACES fx.cmdline
DEPENDENCIES
fx.runtime
fx.collections
fx.term)
+7
View File
@@ -0,0 +1,7 @@
#include <fx/macros.h>
#include <fx/reflection/assembly.h>
FX_ASSEMBLY_BEGIN(fx_cmdline)
FX_ASSEMBLY_NAME("fx.cmdline");
FX_ASSEMBLY_VERSION(1, 0, 0, 0);
FX_ASSEMBLY_END(fx_cmdline)
+4
View File
@@ -0,0 +1,4 @@
add_fx_assembly(
NAME fx.collections
NAMESPACES fx.collections
DEPENDENCIES fx.runtime)
@@ -7,8 +7,7 @@ FX_ASSEMBLY_BEGIN()
FX_ASSEMBLY_EXPORT_TYPE("fx.collections", "array", fx_array); FX_ASSEMBLY_EXPORT_TYPE("fx.collections", "array", fx_array);
FX_ASSEMBLY_EXPORT_TYPE("fx.collections", "bitbuffer", fx_bitbuffer); FX_ASSEMBLY_EXPORT_TYPE("fx.collections", "bitbuffer", fx_bitbuffer);
FX_ASSEMBLY_EXPORT_TYPE("fx.collections", "bitmap", fx_bitmap); FX_ASSEMBLY_EXPORT_TYPE("fx.collections", "bitmap", fx_bitmap);
FX_ASSEMBLY_EXPORT_TYPE("fx.collections", "dict", fx_dict); FX_ASSEMBLY_EXPORT_TYPE("fx.collections", "hashtable", fx_hashtable);
FX_ASSEMBLY_EXPORT_TYPE("fx.collections", "hashmap", fx_hashmap); FX_ASSEMBLY_EXPORT_TYPE("fx.collections", "hashmap", fx_hashmap);
FX_ASSEMBLY_EXPORT_TYPE("fx.collections", "list", fx_list); FX_ASSEMBLY_EXPORT_TYPE("fx.collections", "list", fx_list);
FX_ASSEMBLY_EXPORT_TYPE("fx.collections", "tree", fx_tree);
FX_ASSEMBLY_END() FX_ASSEMBLY_END()
+4
View File
@@ -0,0 +1,4 @@
add_fx_assembly(
NAME fx.compression
NAMESPACES fx.compression
DEPENDENCIES fx.runtime)
+7
View File
@@ -0,0 +1,7 @@
#include <fx/macros.h>
#include <fx/reflection/assembly.h>
FX_ASSEMBLY_BEGIN(fx_compression)
FX_ASSEMBLY_NAME("fx.compression");
FX_ASSEMBLY_VERSION(1, 0, 0, 0);
FX_ASSEMBLY_END(fx_compression)
+4
View File
@@ -0,0 +1,4 @@
add_fx_assembly(
NAME fx.io
NAMESPACES fx.io
DEPENDENCIES fx.runtime)
+7
View File
@@ -0,0 +1,7 @@
#include <fx/macros.h>
#include <fx/reflection/assembly.h>
FX_ASSEMBLY_BEGIN(fx_io)
FX_ASSEMBLY_NAME("fx.io");
FX_ASSEMBLY_VERSION(1, 0, 0, 0);
FX_ASSEMBLY_END(fx_io)
+3
View File
@@ -0,0 +1,3 @@
add_fx_assembly(
NAME fx.runtime
NAMESPACES fx fx.reflection)
@@ -1,7 +1,7 @@
#include <fx/macros.h> #include <fx/macros.h>
#include <fx/reflection/assembly.h> #include <fx/reflection/assembly.h>
FX_ASSEMBLY_BEGIN() FX_ASSEMBLY_BEGIN(fx_runtime)
FX_ASSEMBLY_NAME("fx.runtime"); FX_ASSEMBLY_NAME("fx.runtime");
FX_ASSEMBLY_VERSION(1, 0, 0, 0); FX_ASSEMBLY_VERSION(1, 0, 0, 0);
FX_ASSEMBLY_EXPORT_TYPE("fx", "stream", fx_stream); FX_ASSEMBLY_EXPORT_TYPE("fx", "stream", fx_stream);
@@ -9,4 +9,4 @@ FX_ASSEMBLY_BEGIN()
FX_ASSEMBLY_EXPORT_TYPE("fx", "stringstream", fx_stringstream); FX_ASSEMBLY_EXPORT_TYPE("fx", "stringstream", fx_stringstream);
FX_ASSEMBLY_EXPORT_TYPE("fx", "iterator", fx_iterator); FX_ASSEMBLY_EXPORT_TYPE("fx", "iterator", fx_iterator);
FX_ASSEMBLY_EXPORT_TYPE("fx.reflection", "assembly", fx_assembly); FX_ASSEMBLY_EXPORT_TYPE("fx.reflection", "assembly", fx_assembly);
FX_ASSEMBLY_END() FX_ASSEMBLY_END(fx_runtime)
+4
View File
@@ -0,0 +1,4 @@
add_fx_assembly(
NAME fx.serial
NAMESPACES fx.serial
DEPENDENCIES fx.runtime fx.collections)
+7
View File
@@ -0,0 +1,7 @@
#include <fx/macros.h>
#include <fx/reflection/assembly.h>
FX_ASSEMBLY_BEGIN(fx_serial)
FX_ASSEMBLY_NAME("fx.serial");
FX_ASSEMBLY_VERSION(1, 0, 0, 0);
FX_ASSEMBLY_END(fx_serial)
+4
View File
@@ -0,0 +1,4 @@
add_fx_assembly(
NAME fx.term
NAMESPACES fx.term
DEPENDENCIES fx.runtime fx.collections)
+7
View File
@@ -0,0 +1,7 @@
#include <fx/macros.h>
#include <fx/reflection/assembly.h>
FX_ASSEMBLY_BEGIN(fx_term)
FX_ASSEMBLY_NAME("fx.term");
FX_ASSEMBLY_VERSION(1, 0, 0, 0);
FX_ASSEMBLY_END(fx_term)
+44 -109
View File
@@ -49,43 +49,37 @@ if (FX_STATIC)
set(_lib_suffix "-s") set(_lib_suffix "-s")
endif () endif ()
set(supported_components Core Ds Term Cmd Io Serial Compress) set(assemblies ${FX_FIND_COMPONENTS})
set(components ${FX_FIND_COMPONENTS})
string(REPLACE ";" ", " supported_components_string_list "${supported_components}")
if (NOT components)
set(components ${supported_components})
endif ()
set(required_vars) set(required_vars)
foreach (component ${components}) if (NOT FX_INCLUDE_DIR)
if (NOT "${component}" IN_LIST supported_components) find_path(FX_INCLUDE_DIR
message(FATAL_ERROR "'${component}' is not a valid FX module.\nSupported modules: ${supported_components_string_list}") NAMES fx/misc.h ${FX_FIND_ARGS}
endif ()
string(TOLOWER ${component} header_name)
set(lib_name ${header_name}${_lib_suffix})
if (NOT FX_${component}_INCLUDE_DIR)
find_path(FX_${component}_INCLUDE_DIR
NAMES fx/${header_name}.h ${FX_FIND_ARGS}
PATH_SUFFIXES include PATH_SUFFIXES include
PATHS ${FX_SEARCH_PATHS}) PATHS ${FX_SEARCH_PATHS})
endif () endif ()
if (NOT FX_${component}_LIBRARY) set(required_vars FX_INCLUDE_DIR)
find_library(FX_${component}_LIBRARY
NAMES fx-${lib_name} ${FX_FIND_ARGS} foreach (assembly ${assemblies})
string(TOLOWER ${assembly} header_name)
string(REPLACE "." "_" macro_name ${assembly})
string(TOUPPER ${macro_name} macro_name)
set(lib_name ${assembly}${_lib_suffix})
if (NOT ${macro_name}_LIBRARY)
find_library(${macro_name}_LIBRARY
NAMES ${lib_name} ${FX_FIND_ARGS}
PATH_SUFFIXES lib PATH_SUFFIXES lib
PATHS ${FX_SEARCH_PATHS}) PATHS ${FX_SEARCH_PATHS})
else () else ()
# on Windows, ensure paths are in canonical format (forward slahes): # on Windows, ensure paths are in canonical format (forward slahes):
file(TO_CMAKE_PATH "${FX_${component}_LIBRARY}" FX_${component}_LIBRARY) file(TO_CMAKE_PATH "${${macro_name}_LIBRARY}" ${macro_name}_LIBRARY)
endif() endif()
list(APPEND required_vars FX_${component}_INCLUDE_DIR FX_${component}_LIBRARY) list(APPEND required_vars ${macro_name}_LIBRARY)
endforeach (component) endforeach (assembly)
unset(FX_FIND_ARGS) unset(FX_FIND_ARGS)
@@ -96,94 +90,35 @@ find_package_handle_standard_args(FX
if (FX_FOUND) if (FX_FOUND)
set(created_targets) set(created_targets)
foreach (component ${components}) foreach (assembly ${assemblies})
string(TOLOWER ${component} header_name) set(target_name ${assembly})
set(lib_name ${header_name}${_lib_suffix}) string(REPLACE "fx." "" target_name ${target_name})
string(SUBSTRING ${target_name} 0 1 target_name_prefix)
string(TOUPPER ${target_name_prefix} target_name_prefix)
string(SUBSTRING ${target_name} 1 -1 target_name_suffix)
set(target_name ${target_name_prefix}${target_name_suffix})
if(NOT TARGET FX::${component}) string(TOLOWER ${assembly} header_name)
add_library(FX::${component} UNKNOWN IMPORTED) string(REPLACE "." "_" macro_name ${assembly})
set_target_properties(FX::${component} PROPERTIES string(REPLACE "." "_" macro_name ${assembly})
INTERFACE_INCLUDE_DIRECTORIES "${FX_${component}_INCLUDE_DIR}") string(TOUPPER ${macro_name} macro_name)
target_compile_definitions(FX::${component} INTERFACE _CRT_SECURE_NO_WARNINGS=1)
set(lib_name ${assembly}${_lib_suffix})
if (NOT TARGET FX::${target_name})
add_library(FX::${target_name} UNKNOWN IMPORTED)
set_target_properties(FX::${target_name} PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${FX_INCLUDE_DIR}")
target_compile_definitions(FX::${target_name} INTERFACE _CRT_SECURE_NO_WARNINGS=1)
if (FX_STATIC) if (FX_STATIC)
target_compile_definitions(FX::${component} INTERFACE FX_STATIC=1) target_compile_definitions(FX::${target_name} INTERFACE FX_STATIC=1)
endif () endif ()
set_target_properties(FX::${component} PROPERTIES set_target_properties(FX::${target_name} PROPERTIES
IMPORTED_LINK_INTERFACE_LANGUAGES "C" IMPORTED_LINK_INTERFACE_LANGUAGES "C"
IMPORTED_LOCATION "${FX_${component}_LIBRARY}") IMPORTED_LOCATION "${${macro_name}_LIBRARY}")
set(created_targets ${created_targets} ${component}) set(created_targets ${created_targets} ${assembly})
endif () endif ()
endforeach (component) endforeach (assembly)
foreach (component ${created_targets})
if ("${component}" STREQUAL "Ds")
if (NOT TARGET FX::Core)
message(FATAL_ERROR "FX: Module 'Ds' depends on 'Core', which was not specified in find_package()")
endif ()
target_link_libraries(FX::Ds INTERFACE FX::Core)
endif ()
if ("${component}" STREQUAL "Term")
if (NOT TARGET FX::Core)
message(FATAL_ERROR "FX: Module 'Term' depends on 'Core', which was not specified in find_package()")
endif ()
if (NOT TARGET FX::Ds)
message(FATAL_ERROR "FX: Module 'Term' depends on 'Ds', which was not specified in find_package()")
endif ()
target_link_libraries(FX::Term INTERFACE FX::Core FX::Ds)
endif ()
if ("${component}" STREQUAL "Serial")
if (NOT TARGET FX::Core)
message(FATAL_ERROR "FX: Module 'Serial' depends on 'Core', which was not specified in find_package()")
endif ()
if (NOT TARGET FX::Ds)
message(FATAL_ERROR "FX: Module 'Serial' depends on 'Ds', which was not specified in find_package()")
endif ()
target_link_libraries(FX::Serial INTERFACE FX::Core FX::Ds)
endif ()
if ("${component}" STREQUAL "Cmd")
if (NOT TARGET FX::Core)
message(FATAL_ERROR "FX: Module 'Cmd' depends on 'Core', which was not specified in find_package()")
endif ()
if (NOT TARGET FX::Ds)
message(FATAL_ERROR "FX: Module 'Cmd' depends on 'Ds', which was not specified in find_package()")
endif ()
if (NOT TARGET FX::Term)
message(FATAL_ERROR "FX: Module 'Cmd' depends on 'Term', which was not specified in find_package()")
endif ()
target_link_libraries(FX::Cmd INTERFACE FX::Core FX::Ds FX::Term)
endif ()
if ("${component}" STREQUAL "Io")
if (NOT TARGET FX::Core)
message(FATAL_ERROR "FX: Module 'Io' depends on 'Core', which was not specified in find_package()")
endif ()
if (NOT TARGET FX::Ds)
message(FATAL_ERROR "FX: Module 'Io' depends on 'Ds', which was not specified in find_package()")
endif ()
target_link_libraries(FX::Io INTERFACE FX::Core FX::Ds)
endif ()
if ("${component}" STREQUAL "Compress")
if (NOT TARGET FX::Core)
message(FATAL_ERROR "FX: Module 'Compress' depends on 'Core', which was not specified in find_package()")
endif ()
target_link_libraries(FX::Compress INTERFACE FX::Core FX::Ds)
endif ()
endforeach (component)
endif() endif()
+36
View File
@@ -0,0 +1,36 @@
function(get_platform_details)
set(options)
set(one_value_args SYSTEM PROCESSOR)
set(multi_value_args
NAMESPACES
DEPENDENCIES
LIBS)
cmake_parse_arguments(PARSE_ARGV 0 arg
"${options}"
"${one_value_args}"
"${multi_value_args}")
set(system_name "")
if ("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux")
set(system_name "linux")
elseif ("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin")
set(system_name "apple")
elseif ("${CMAKE_SYSTEM_NAME}" STREQUAL "Windows")
set(system_name "win32")
else ()
string(TOLOWER "${CMAKE_SYSTEM_NAME}" system_name)
endif ()
if ("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "x86_64")
set(system_arch "amd64")
elseif ("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "arm64")
set(system_arch "aarch64")
else ()
string(TOLOWER "${CMAKE_SYSTEM_PROCESSOR}" system_arch)
endif ()
set(${arg_SYSTEM} ${system_name} PARENT_SCOPE)
set(${arg_PROCESSOR} ${system_arch} PARENT_SCOPE)
endfunction (get_platform_details)
+45 -35
View File
@@ -17,12 +17,13 @@ function(add_fx_assembly)
string(TOUPPER ${assembly_token} assembly_token) string(TOUPPER ${assembly_token} assembly_token)
set(assembly_include_paths "") set(assembly_include_paths "")
set(assembly_sources set(assembly_sources ${CMAKE_CURRENT_SOURCE_DIR}/assembly.c)
${CMAKE_CURRENT_SOURCE_DIR}/assemblies/${assembly_name}.c) set(assembly_headers)
foreach (dir ${arg_NAMESPACES}) foreach (dir ${arg_NAMESPACES})
add_subdirectory(${dir}) add_subdirectory(${fx_source_root}/${dir} ${fx_build_root}/namespaces/${dir})
set(assembly_sources ${assembly_sources} ${namespace_sources}) set(assembly_sources ${assembly_sources} ${namespace_sources})
set(assembly_headers ${assembly_headers} ${namespace_headers})
set(assembly_include_paths ${assembly_include_paths} ${namespace_include_paths}) set(assembly_include_paths ${assembly_include_paths} ${namespace_include_paths})
endforeach (dir) endforeach (dir)
@@ -39,62 +40,71 @@ function(add_fx_assembly)
set_target_properties(${assembly_target_name} set_target_properties(${assembly_target_name}
PROPERTIES POSITION_INDEPENDENT_CODE ON) PROPERTIES POSITION_INDEPENDENT_CODE ON)
foreach (dep ${arg_DEPENDENCIES}) target_link_libraries(${assembly_target_name} ${internal_libs} ${arg_DEPENDENCIES})
target_link_libraries(${assembly_target_name} ${dep}) target_include_directories(${assembly_target_name} PRIVATE ${internal_include_dirs})
endforeach (dep)
foreach (lib ${arg_LIBS})
target_link_libraries(${assembly_target_name} ${lib})
endforeach (lib)
foreach (dir ${arg_INCLUDE_DIRS})
target_include_directories(${assembly_target_name} PRIVATE ${dir})
endforeach (dir)
foreach (def ${arg_DEFINES}) foreach (def ${arg_DEFINES})
target_compile_definitions(${assembly_target_name} PRIVATE ${def}) target_compile_definitions(${assembly_target_name} PRIVATE ${def})
endforeach (def) endforeach (def)
if (fx_enable_tests)
foreach (ns ${arg_NAMESPACES})
file(GLOB test_sources ${fx_source_root}/${ns}/test/*.c)
foreach (test_file ${test_sources})
get_filename_component(test_name ${test_file} NAME_WE)
set(test_name ${ns}-${test_name})
string(REPLACE "." "-" test_name ${test_name})
add_executable(${test_name} ${test_file})
target_link_libraries(${test_name} ${assembly_target_name})
endforeach (test_file)
endforeach (ns)
endif ()
set_target_properties(${assembly_target_name} PROPERTIES set_target_properties(${assembly_target_name} PROPERTIES
FOLDER "${assembly_name}") FOLDER "${assembly_name}")
TEST_BIG_ENDIAN(IS_BIG_ENDIAN)
if(IS_BIG_ENDIAN)
target_compile_definitions(${assembly_target_name} PRIVATE
BIG_ENDIAN)
else()
target_compile_definitions(${assembly_target_name} PRIVATE
LITTLE_ENDIAN)
endif()
install(TARGETS ${assembly_target_name}) install(TARGETS ${assembly_target_name})
install(FILES ${headers} DESTINATION include/${assembly_path})
endfunction(add_fx_assembly) endfunction(add_fx_assembly)
macro(export_fx_namespace_details) macro(export_fx_namespace_details ns_name)
set(options) set(namespace_name ${ns_name})
set(one_value_args NAME)
set(multi_value_args)
cmake_parse_arguments(PARSE_ARGV 0 arg
"${options}"
"${one_value_args}"
"${multi_value_args}")
set(namespace_name ${arg_NAME})
string(REPLACE "." "/" namespace_path ${namespace_name}) string(REPLACE "." "/" namespace_path ${namespace_name})
set(namespace_target_name ${namespace_name}) set(namespace_target_name ${namespace_name})
set(namespace_include_paths ${CMAKE_CURRENT_SOURCE_DIR}/include PARENT_SCOPE) set(namespace_include_paths ${CMAKE_CURRENT_SOURCE_DIR}/include PARENT_SCOPE)
file(GLOB namespace_sources file(GLOB base_namespace_sources
*.c *.h *.c *.h
${CMAKE_CURRENT_SOURCE_DIR}/include/${namespace_path}/*.h) ${CMAKE_CURRENT_SOURCE_DIR}/include/${namespace_path}/*.h)
file(GLOB sys_sources sys/${fx_system_name}/*.c sys/${fx_system_name}/*.h)
foreach (dir ${source_dirs})
file(GLOB dir_sources ${dir}/*.c ${dir}/*.h)
set(dir_namespace_sources ${dir_namespace_sources} ${dir_sources})
endforeach (dir)
file(GLOB sys_sources
${CMAKE_CURRENT_SOURCE_DIR}/sys/${fx_system_name}/*.c
${CMAKE_CURRENT_SOURCE_DIR}/sys/${fx_system_name}/*.h
${CMAKE_CURRENT_SOURCE_DIR}/sys/${fx_system_name}/${fx_system_arch}/*.c
${CMAKE_CURRENT_SOURCE_DIR}/sys/${fx_system_name}/${fx_system_arch}/*.h
${CMAKE_CURRENT_SOURCE_DIR}/sys/${fx_system_name}/${fx_system_arch}/*.S)
file(GLOB headers include/${namespace_path}/*.h) file(GLOB headers include/${namespace_path}/*.h)
set(namespace_headers ${headers} PARENT_SCOPE)
set(namespace_sources set(namespace_sources
${namespace_sources} ${namespace_sources}
${base_namespace_sources}
${dir_namespace_sources}
${sys_sources} ${sys_sources}
${headers} ${headers}
PARENT_SCOPE) PARENT_SCOPE)
set(internal_include_dirs
${internal_include_dirs}
${CMAKE_CURRENT_SOURCE_DIR}/sys/${fx_system_name}/include
${CMAKE_CURRENT_SOURCE_DIR}/sys/${fx_system_name}/${fx_system_arch}/include
PARENT_SCOPE)
set(internal_libs ${internal_libs} PARENT_SCOPE)
set(internal_defines ${internal_defines} PARENT_SCOPE)
install(FILES ${headers} DESTINATION include/${namespace_path})
endmacro(export_fx_namespace_details) endmacro(export_fx_namespace_details)
+1 -3
View File
@@ -1,3 +1 @@
include(../cmake/Templates.cmake) export_fx_namespace_details(fx.cmdline)
add_fx_module(NAME cmd DEPENDENCIES core ds term)
+21 -9
View File
@@ -1,7 +1,7 @@
#include "command.h" #include "command.h"
#include <fx/cmd.h> #include <fx/cmdline/cmd.h>
#include <fx/ds/string.h> #include <fx/string.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@@ -54,7 +54,8 @@ fx_status fx_command_arg_set_name(struct fx_command_arg *arg, const char *name)
} }
fx_status fx_command_arg_set_description( fx_status fx_command_arg_set_description(
struct fx_command_arg *arg, const char *description) struct fx_command_arg *arg,
const char *description)
{ {
char *desc = fx_strdup(description); char *desc = fx_strdup(description);
if (!desc) { if (!desc) {
@@ -71,14 +72,16 @@ fx_status fx_command_arg_set_description(
} }
fx_status fx_command_arg_set_nr_values( fx_status fx_command_arg_set_nr_values(
struct fx_command_arg *arg, enum fx_command_arg_value_count nr_values) struct fx_command_arg *arg,
enum fx_command_arg_value_count nr_values)
{ {
arg->arg_nr_values = nr_values; arg->arg_nr_values = nr_values;
return FX_SUCCESS; return FX_SUCCESS;
} }
fx_status fx_command_arg_set_allowed_values( fx_status fx_command_arg_set_allowed_values(
struct fx_command_arg *arg, const char **allowed_values) struct fx_command_arg *arg,
const char **allowed_values)
{ {
size_t count; size_t count;
for (count = 0; allowed_values[count]; count++) for (count = 0; allowed_values[count]; count++)
@@ -102,7 +105,10 @@ fx_status fx_command_arg_set_allowed_values(
return FX_SUCCESS; return FX_SUCCESS;
} }
void z__fx_get_arg_usage_string(struct fx_command_arg *arg, bool colour, fx_string *out) void z__fx_get_arg_usage_string(
struct fx_command_arg *arg,
bool colour,
fx_string *out)
{ {
bool optional = false, multi = false; bool optional = false, multi = false;
switch (arg->arg_nr_values) { switch (arg->arg_nr_values) {
@@ -126,10 +132,14 @@ void z__fx_get_arg_usage_string(struct fx_command_arg *arg, bool colour, fx_stri
if (optional) { if (optional) {
fx_string_append_cstrf( fx_string_append_cstrf(
out, colour ? F_GREEN "[[%s]" : "[[%s]", arg->arg_name); out,
colour ? F_GREEN "[[%s]" : "[[%s]",
arg->arg_name);
} else { } else {
fx_string_append_cstrf( fx_string_append_cstrf(
out, colour ? F_GREEN "<%s>" : "<%s>", arg->arg_name); out,
colour ? F_GREEN "<%s>" : "<%s>",
arg->arg_name);
} }
for (int i = 1; i < arg->arg_nr_values; i++) { for (int i = 1; i < arg->arg_nr_values; i++) {
@@ -167,7 +177,9 @@ void z__fx_get_arg_description(struct fx_command_arg *arg, fx_string *out)
} }
fx_string_append_cstrf( fx_string_append_cstrf(
out, " " F_GREEN "%s" F_RESET, arg->arg_allowed_values[i]); out,
" " F_GREEN "%s" F_RESET,
arg->arg_allowed_values[i]);
} }
fx_string_append_cstr(out, "]"); fx_string_append_cstr(out, "]");
+134 -54
View File
@@ -1,8 +1,8 @@
#include "command.h" #include "command.h"
#include <fx/cmd.h> #include <fx/cmdline/cmd.h>
#include <fx/core/misc.h> #include <fx/misc.h>
#include <fx/ds/string.h> #include <fx/string.h>
#include <fx/term/print.h> #include <fx/term/print.h>
#include <fx/term/tty.h> #include <fx/term/tty.h>
#include <stdarg.h> #include <stdarg.h>
@@ -11,14 +11,28 @@
#include <string.h> #include <string.h>
FX_BST_DEFINE_SIMPLE_INSERT( FX_BST_DEFINE_SIMPLE_INSERT(
struct fx_arglist_option, opt_node, opt_id, put_arglist_option) struct fx_arglist_option,
opt_node,
opt_id,
put_arglist_option)
FX_BST_DEFINE_SIMPLE_GET( FX_BST_DEFINE_SIMPLE_GET(
struct fx_arglist_option, unsigned int, opt_node, opt_id, get_arglist_option) struct fx_arglist_option,
unsigned int,
opt_node,
opt_id,
get_arglist_option)
FX_BST_DEFINE_SIMPLE_INSERT( FX_BST_DEFINE_SIMPLE_INSERT(
struct fx_arglist_value, val_node, val_id, put_arglist_value) struct fx_arglist_value,
val_node,
val_id,
put_arglist_value)
FX_BST_DEFINE_SIMPLE_GET( FX_BST_DEFINE_SIMPLE_GET(
struct fx_arglist_value, unsigned int, val_node, val_id, get_arglist_value) struct fx_arglist_value,
unsigned int,
val_node,
val_id,
get_arglist_value)
struct argv_parser { struct argv_parser {
struct fx_command *cmd; struct fx_command *cmd;
@@ -62,7 +76,9 @@ struct fx_arglist *fx_arglist_create(void)
return out; return out;
} }
static void move_to_subcommand(struct argv_parser *parser, struct fx_command *cmd) static void move_to_subcommand(
struct argv_parser *parser,
struct fx_command *cmd)
{ {
parser->cmd = cmd; parser->cmd = cmd;
parser->arglist->list_command = cmd; parser->arglist->list_command = cmd;
@@ -86,10 +102,13 @@ static fx_status set_opt(struct fx_arglist *args, struct fx_command_option *opt)
} }
static fx_status put_arg( static fx_status put_arg(
struct fx_arglist *args, struct fx_command_arg *arg, const char *value) struct fx_arglist *args,
struct fx_command_arg *arg,
const char *value)
{ {
struct fx_arglist_option *arglist_opt struct fx_arglist_option *arglist_opt = get_arglist_option(
= get_arglist_option(&args->list_options, FX_COMMAND_INVALID_ID); &args->list_options,
FX_COMMAND_INVALID_ID);
if (!arglist_opt) { if (!arglist_opt) {
arglist_opt = malloc(sizeof *arglist_opt); arglist_opt = malloc(sizeof *arglist_opt);
@@ -134,8 +153,10 @@ static fx_status put_arg(
} }
static fx_status put_opt_arg( static fx_status put_opt_arg(
struct fx_arglist_option *arglist_opt, struct fx_command_option *opt, struct fx_arglist_option *arglist_opt,
struct fx_command_arg *arg, const char *value) struct fx_command_option *opt,
struct fx_command_arg *arg,
const char *value)
{ {
if (arg->arg_allowed_values) { if (arg->arg_allowed_values) {
@@ -202,14 +223,18 @@ static fx_status check_required_args(struct argv_parser *parser)
} }
fx_arglist_report_missing_args( fx_arglist_report_missing_args(
parser->arglist, FX_COMMAND_INVALID_ID, arg->arg_id, parser->arglist,
FX_COMMAND_INVALID_ID,
arg->arg_id,
parser->nr_values_cur_arg); parser->nr_values_cur_arg);
return FX_ERR_BAD_FORMAT; return FX_ERR_BAD_FORMAT;
} }
if (parser->nr_values_cur_arg != arg->arg_nr_values) { if (parser->nr_values_cur_arg != arg->arg_nr_values) {
fx_arglist_report_missing_args( fx_arglist_report_missing_args(
parser->arglist, FX_COMMAND_INVALID_ID, arg->arg_id, parser->arglist,
FX_COMMAND_INVALID_ID,
arg->arg_id,
parser->nr_values_cur_arg); parser->nr_values_cur_arg);
return FX_ERR_BAD_FORMAT; return FX_ERR_BAD_FORMAT;
} }
@@ -225,8 +250,9 @@ static fx_status parse_arg(struct argv_parser *parser)
break; break;
} }
struct fx_command *subcmd struct fx_command *subcmd = fx_command_get_subcommand_with_name(
= fx_command_get_subcommand_with_name(parser->cmd, value); parser->cmd,
value);
if (subcmd) { if (subcmd) {
move_to_subcommand(parser, subcmd); move_to_subcommand(parser, subcmd);
parser->arglist->list_argv_last_command parser->arglist->list_argv_last_command
@@ -236,10 +262,14 @@ static fx_status parse_arg(struct argv_parser *parser)
} }
struct fx_command_arg *arg = fx_unbox( struct fx_command_arg *arg = fx_unbox(
struct fx_command_arg, parser->arg_it, arg_entry); struct fx_command_arg,
parser->arg_it,
arg_entry);
if (!arg) { if (!arg) {
fx_arglist_report_unexpected_arg(parser->arglist, value); fx_arglist_report_unexpected_arg(
parser->arglist,
value);
return FX_ERR_BAD_FORMAT; return FX_ERR_BAD_FORMAT;
} }
@@ -249,8 +279,10 @@ static fx_status parse_arg(struct argv_parser *parser)
if (status == FX_ERR_INVALID_ARGUMENT) { if (status == FX_ERR_INVALID_ARGUMENT) {
fx_arglist_report_invalid_arg_value( fx_arglist_report_invalid_arg_value(
parser->arglist, FX_COMMAND_INVALID_ID, parser->arglist,
arg->arg_id, value); FX_COMMAND_INVALID_ID,
arg->arg_id,
value);
} }
if (FX_ERR(status)) { if (FX_ERR(status)) {
@@ -306,7 +338,8 @@ static fx_status parse_short_opt(struct argv_parser *parser)
opt = fx_command_get_option_with_short_name(parser->cmd, flag); opt = fx_command_get_option_with_short_name(parser->cmd, flag);
if (!opt) { if (!opt) {
subcmd = fx_command_get_subcommand_with_short_name( subcmd = fx_command_get_subcommand_with_short_name(
parser->cmd, flag); parser->cmd,
flag);
} }
if (subcmd) { if (subcmd) {
@@ -319,10 +352,13 @@ static fx_status parse_short_opt(struct argv_parser *parser)
if (!opt) { if (!opt) {
fx_string *usage = z__fx_command_default_usage_string( fx_string *usage = z__fx_command_default_usage_string(
parser->cmd, NULL, parser->arglist); parser->cmd,
NULL,
parser->arglist);
fx_err("unrecognised argument '" F_YELLOW "-%c" F_RESET fx_err("unrecognised argument '" F_YELLOW "-%c" F_RESET
"'\n\n", "'\n\n",
flag, fx_string_get_cstr(usage)); flag,
fx_string_get_cstr(usage));
fx_i("usage: %s", fx_string_get_cstr(usage)); fx_i("usage: %s", fx_string_get_cstr(usage));
fx_i("for more information, use '" F_YELLOW fx_i("for more information, use '" F_YELLOW
"--help" F_RESET "'\n"); "--help" F_RESET "'\n");
@@ -382,8 +418,10 @@ static fx_status parse_short_opt(struct argv_parser *parser)
if (status == FX_ERR_INVALID_ARGUMENT) { if (status == FX_ERR_INVALID_ARGUMENT) {
fx_arglist_report_invalid_arg_value( fx_arglist_report_invalid_arg_value(
parser->arglist, FX_COMMAND_INVALID_ID, parser->arglist,
arg->arg_id, value); FX_COMMAND_INVALID_ID,
arg->arg_id,
value);
} }
if (FX_ERR(status)) { if (FX_ERR(status)) {
@@ -408,7 +446,9 @@ static fx_status parse_short_opt(struct argv_parser *parser)
} }
fx_arglist_report_missing_args( fx_arglist_report_missing_args(
parser->arglist, opt->opt_id, arg->arg_id, parser->arglist,
opt->opt_id,
arg->arg_id,
nr_args_cur_opt); nr_args_cur_opt);
return FX_ERR_BAD_FORMAT; return FX_ERR_BAD_FORMAT;
} }
@@ -420,7 +460,9 @@ static fx_status parse_short_opt(struct argv_parser *parser)
if (!value) { if (!value) {
fx_arglist_report_missing_args( fx_arglist_report_missing_args(
parser->arglist, opt->opt_id, arg->arg_id, parser->arglist,
opt->opt_id,
arg->arg_id,
nr_args_cur_opt); nr_args_cur_opt);
return FX_ERR_BAD_FORMAT; return FX_ERR_BAD_FORMAT;
} }
@@ -443,16 +485,20 @@ static fx_status parse_long_opt(struct argv_parser *parser)
opt = fx_command_get_option_with_long_name(parser->cmd, opt_name); opt = fx_command_get_option_with_long_name(parser->cmd, opt_name);
if (!opt) { if (!opt) {
subcmd = fx_command_get_subcommand_with_long_name( subcmd = fx_command_get_subcommand_with_long_name(
parser->cmd, opt_name); parser->cmd,
opt_name);
} }
if (!opt && !subcmd) { if (!opt && !subcmd) {
fx_string *usage = z__fx_command_default_usage_string( fx_string *usage = z__fx_command_default_usage_string(
parser->cmd, NULL, parser->arglist); parser->cmd,
NULL,
parser->arglist);
fx_err("unrecognised argument '" F_YELLOW "--%s" F_RESET fx_err("unrecognised argument '" F_YELLOW "--%s" F_RESET
"'\n\nusage: %s\n\nfor more information, use '" F_YELLOW "'\n\nusage: %s\n\nfor more information, use '" F_YELLOW
"--help" F_RESET "'\n", "--help" F_RESET "'\n",
opt_name, fx_string_get_cstr(usage)); opt_name,
fx_string_get_cstr(usage));
fx_string_unref(usage); fx_string_unref(usage);
return FX_ERR_NO_ENTRY; return FX_ERR_NO_ENTRY;
@@ -504,8 +550,10 @@ static fx_status parse_long_opt(struct argv_parser *parser)
if (status == FX_ERR_INVALID_ARGUMENT) { if (status == FX_ERR_INVALID_ARGUMENT) {
fx_arglist_report_invalid_arg_value( fx_arglist_report_invalid_arg_value(
parser->arglist, FX_COMMAND_INVALID_ID, parser->arglist,
arg->arg_id, value); FX_COMMAND_INVALID_ID,
arg->arg_id,
value);
} }
if (FX_ERR(status)) { if (FX_ERR(status)) {
@@ -533,7 +581,9 @@ static fx_status parse_long_opt(struct argv_parser *parser)
} }
fx_arglist_report_missing_args( fx_arglist_report_missing_args(
parser->arglist, opt->opt_id, arg->arg_id, parser->arglist,
opt->opt_id,
arg->arg_id,
nr_args_cur_opt); nr_args_cur_opt);
return FX_ERR_BAD_FORMAT; return FX_ERR_BAD_FORMAT;
} }
@@ -546,7 +596,9 @@ static fx_status parse_long_opt(struct argv_parser *parser)
if (!value) { if (!value) {
fx_arglist_report_missing_args( fx_arglist_report_missing_args(
parser->arglist, opt->opt_id, arg->arg_id, parser->arglist,
opt->opt_id,
arg->arg_id,
nr_args_cur_opt); nr_args_cur_opt);
return FX_ERR_BAD_FORMAT; return FX_ERR_BAD_FORMAT;
} }
@@ -580,7 +632,9 @@ static bool should_show_help(struct fx_command *cmd, struct fx_arglist *args)
} }
fx_status fx_arglist_parse( fx_status fx_arglist_parse(
struct fx_arglist *args, struct fx_command **cmd, int argc, struct fx_arglist *args,
struct fx_command **cmd,
int argc,
const char **argv) const char **argv)
{ {
struct argv_parser parser = { struct argv_parser parser = {
@@ -665,7 +719,9 @@ void fx_arglist_destroy(struct fx_arglist *args)
args_it = fx_bst_first(&opt->opt_values); args_it = fx_bst_first(&opt->opt_values);
while (args_it) { while (args_it) {
struct fx_arglist_value *val = fx_unbox( struct fx_arglist_value *val = fx_unbox(
struct fx_arglist_value, args_it, val_node); struct fx_arglist_value,
args_it,
val_node);
args_next = fx_bst_next(args_it); args_next = fx_bst_next(args_it);
if (val) { if (val) {
@@ -686,8 +742,11 @@ void fx_arglist_destroy(struct fx_arglist *args)
} }
fx_status fx_arglist_get_string( fx_status fx_arglist_get_string(
const fx_arglist *args, unsigned int opt_id, unsigned int arg_id, const fx_arglist *args,
unsigned int index, const char **out) unsigned int opt_id,
unsigned int arg_id,
unsigned int index,
const char **out)
{ {
fx_arglist_iterator it = {0}; fx_arglist_iterator it = {0};
fx_arglist_iterator_begin(args, opt_id, arg_id, &it); fx_arglist_iterator_begin(args, opt_id, arg_id, &it);
@@ -710,8 +769,11 @@ fx_status fx_arglist_get_string(
} }
fx_status fx_arglist_get_int( fx_status fx_arglist_get_int(
const fx_arglist *args, unsigned int opt_id, unsigned int arg_id, const fx_arglist *args,
unsigned int index, long long *out) unsigned int opt_id,
unsigned int arg_id,
unsigned int index,
long long *out)
{ {
fx_arglist_iterator it = {0}; fx_arglist_iterator it = {0};
fx_arglist_iterator_begin(args, opt_id, arg_id, &it); fx_arglist_iterator_begin(args, opt_id, arg_id, &it);
@@ -734,8 +796,11 @@ fx_status fx_arglist_get_int(
} }
fx_status fx_arglist_get_uint( fx_status fx_arglist_get_uint(
const fx_arglist *args, unsigned int opt_id, unsigned int arg_id, const fx_arglist *args,
unsigned int index, unsigned long long *out) unsigned int opt_id,
unsigned int arg_id,
unsigned int index,
unsigned long long *out)
{ {
fx_arglist_iterator it = {0}; fx_arglist_iterator it = {0};
fx_arglist_iterator_begin(args, opt_id, arg_id, &it); fx_arglist_iterator_begin(args, opt_id, arg_id, &it);
@@ -758,7 +823,9 @@ fx_status fx_arglist_get_uint(
} }
fx_status fx_arglist_get_option( fx_status fx_arglist_get_option(
const fx_arglist *args, unsigned int opt_id, unsigned int index, const fx_arglist *args,
unsigned int opt_id,
unsigned int index,
fx_arglist_option **out) fx_arglist_option **out)
{ {
struct fx_bst_node *node = fx_bst_first(&args->list_options); struct fx_bst_node *node = fx_bst_first(&args->list_options);
@@ -785,7 +852,9 @@ fx_status fx_arglist_get_option(
} }
size_t fx_arglist_get_count( size_t fx_arglist_get_count(
const fx_arglist *args, unsigned int opt_id, unsigned int arg_id) const fx_arglist *args,
unsigned int opt_id,
unsigned int arg_id)
{ {
size_t count = 0; size_t count = 0;
fx_arglist_iterator it; fx_arglist_iterator it;
@@ -799,12 +868,15 @@ size_t fx_arglist_get_count(
} }
fx_status fx_arglist_option_get_value( fx_status fx_arglist_option_get_value(
const fx_arglist_option *opt, unsigned int arg_id, unsigned int index, const fx_arglist_option *opt,
unsigned int arg_id,
unsigned int index,
fx_arglist_value **out) fx_arglist_value **out)
{ {
struct fx_bst_node *node = fx_bst_first(&opt->opt_values); struct fx_bst_node *node = fx_bst_first(&opt->opt_values);
while (node) { while (node) {
fx_arglist_value *cur = fx_unbox(fx_arglist_value, node, val_node); fx_arglist_value *cur
= fx_unbox(fx_arglist_value, node, val_node);
if (cur->val_id != arg_id) { if (cur->val_id != arg_id) {
node = fx_bst_next(node); node = fx_bst_next(node);
@@ -825,7 +897,8 @@ fx_status fx_arglist_option_get_value(
/************************ arglist iterator functions **************************/ /************************ arglist iterator functions **************************/
static struct fx_arglist_option *advance_to_next_opt(struct fx_arglist_iterator *it) static struct fx_arglist_option *advance_to_next_opt(
struct fx_arglist_iterator *it)
{ {
struct fx_arglist_option *opt; struct fx_arglist_option *opt;
@@ -846,7 +919,8 @@ static struct fx_arglist_option *advance_to_next_opt(struct fx_arglist_iterator
return NULL; return NULL;
} }
static struct fx_arglist_value *advance_to_next_arg(struct fx_arglist_iterator *it) static struct fx_arglist_value *advance_to_next_arg(
struct fx_arglist_iterator *it)
{ {
struct fx_arglist_value *val; struct fx_arglist_value *val;
@@ -868,8 +942,10 @@ static struct fx_arglist_value *advance_to_next_arg(struct fx_arglist_iterator *
} }
int fx_arglist_iterator_begin( int fx_arglist_iterator_begin(
const struct fx_arglist *args, unsigned int opt_filter, const struct fx_arglist *args,
unsigned int arg_filter, struct fx_arglist_iterator *it) unsigned int opt_filter,
unsigned int arg_filter,
struct fx_arglist_iterator *it)
{ {
memset(it, 0x0, sizeof *it); memset(it, 0x0, sizeof *it);
@@ -909,7 +985,9 @@ int fx_arglist_iterator_begin(
} }
val = fx_unbox( val = fx_unbox(
struct fx_arglist_value, it->_arg_it, val_node); struct fx_arglist_value,
it->_arg_it,
val_node);
if (!val if (!val
|| (arg_filter != val->val_id || (arg_filter != val->val_id
&& arg_filter != FX_COMMAND_INVALID_ID)) { && arg_filter != FX_COMMAND_INVALID_ID)) {
@@ -994,7 +1072,8 @@ static struct fx_arglist_option *advance_to_next_opt2(
} }
int fx_arglist_option_iterator_begin( int fx_arglist_option_iterator_begin(
const struct fx_arglist *args, unsigned int opt_filter, const struct fx_arglist *args,
unsigned int opt_filter,
struct fx_arglist_option_iterator *it) struct fx_arglist_option_iterator *it)
{ {
memset(it, 0x0, sizeof *it); memset(it, 0x0, sizeof *it);
@@ -1047,7 +1126,8 @@ bool fx_arglist_option_iterator_next(struct fx_arglist_option_iterator *it)
return true; return true;
} }
bool fx_arglist_option_iterator_is_valid(const struct fx_arglist_option_iterator *it) bool fx_arglist_option_iterator_is_valid(
const struct fx_arglist_option_iterator *it)
{ {
return it->opt_id != FX_COMMAND_INVALID_ID || it->opt != NULL; return it->opt_id != FX_COMMAND_INVALID_ID || it->opt != NULL;
} }
+137 -59
View File
@@ -1,8 +1,8 @@
#include "command.h" #include "command.h"
#include <fx/cmd.h> #include <fx/bst.h>
#include <fx/core/bst.h> #include <fx/cmdline/cmd.h>
#include <fx/ds/string.h> #include <fx/string.h>
#include <fx/term/print.h> #include <fx/term/print.h>
#include <fx/term/tty.h> #include <fx/term/tty.h>
#include <stdio.h> #include <stdio.h>
@@ -13,7 +13,12 @@
static struct fx_bst command_list = {0}; static struct fx_bst command_list = {0};
FX_BST_DEFINE_SIMPLE_GET(struct fx_command, unsigned int, c_node, c_id, get_command) FX_BST_DEFINE_SIMPLE_GET(
struct fx_command,
unsigned int,
c_node,
c_id,
get_command)
FX_BST_DEFINE_SIMPLE_INSERT(struct fx_command, c_node, c_id, put_command) FX_BST_DEFINE_SIMPLE_INSERT(struct fx_command, c_node, c_id, put_command)
enum item_type { enum item_type {
@@ -26,7 +31,8 @@ static void command_list_cleanup(void)
{ {
struct fx_bst_node *node = fx_bst_first(&command_list); struct fx_bst_node *node = fx_bst_first(&command_list);
while (node) { while (node) {
struct fx_command *cmd = fx_unbox(struct fx_command, node, c_node); struct fx_command *cmd
= fx_unbox(struct fx_command, node, c_node);
if (!cmd) { if (!cmd) {
break; break;
} }
@@ -57,8 +63,10 @@ static void command_usage_destroy(struct fx_command_usage *usage)
{ {
struct fx_queue_entry *entry = fx_queue_first(&usage->u_parts); struct fx_queue_entry *entry = fx_queue_first(&usage->u_parts);
while (entry) { while (entry) {
struct fx_command_usage_entry *arg struct fx_command_usage_entry *arg = fx_unbox(
= fx_unbox(struct fx_command_usage_entry, entry, e_entry); struct fx_command_usage_entry,
entry,
e_entry);
if (!arg) { if (!arg) {
continue; continue;
} }
@@ -216,7 +224,9 @@ fx_status fx_command_set_flags(struct fx_command *cmd, fx_command_flags flags)
return FX_SUCCESS; return FX_SUCCESS;
} }
fx_status fx_command_set_description(struct fx_command *cmd, const char *description) fx_status fx_command_set_description(
struct fx_command *cmd,
const char *description)
{ {
char *desc = fx_strdup(description); char *desc = fx_strdup(description);
if (!desc) { if (!desc) {
@@ -233,7 +243,8 @@ fx_status fx_command_set_description(struct fx_command *cmd, const char *descrip
} }
fx_status fx_command_set_callback( fx_status fx_command_set_callback(
struct fx_command *cmd, fx_command_callback callback) struct fx_command *cmd,
fx_command_callback callback)
{ {
cmd->c_callback = callback; cmd->c_callback = callback;
return FX_SUCCESS; return FX_SUCCESS;
@@ -284,7 +295,8 @@ struct fx_command_usage *fx_command_add_usage(struct fx_command *cmd)
} }
const struct fx_command_option *fx_command_get_option( const struct fx_command_option *fx_command_get_option(
const struct fx_command *cmd, int id) const struct fx_command *cmd,
int id)
{ {
struct fx_queue_entry *cur = fx_queue_first(&cmd->c_opt); struct fx_queue_entry *cur = fx_queue_first(&cmd->c_opt);
while (cur) { while (cur) {
@@ -302,7 +314,8 @@ const struct fx_command_option *fx_command_get_option(
} }
fx_status fx_command_usage_add_option( fx_status fx_command_usage_add_option(
struct fx_command_usage *usage, struct fx_command_option *opt) struct fx_command_usage *usage,
struct fx_command_option *opt)
{ {
struct fx_command_usage_entry *u_opt = malloc(sizeof *u_opt); struct fx_command_usage_entry *u_opt = malloc(sizeof *u_opt);
if (!u_opt) { if (!u_opt) {
@@ -319,7 +332,8 @@ fx_status fx_command_usage_add_option(
} }
fx_status fx_command_usage_add_arg( fx_status fx_command_usage_add_arg(
struct fx_command_usage *usage, struct fx_command_arg *arg) struct fx_command_usage *usage,
struct fx_command_arg *arg)
{ {
struct fx_command_usage_entry *u_arg = malloc(sizeof *u_arg); struct fx_command_usage_entry *u_arg = malloc(sizeof *u_arg);
if (!u_arg) { if (!u_arg) {
@@ -335,7 +349,9 @@ fx_status fx_command_usage_add_arg(
return FX_SUCCESS; return FX_SUCCESS;
} }
fx_status fx_command_usage_add_command(fx_command_usage *usage, unsigned int cmd_id) fx_status fx_command_usage_add_command(
fx_command_usage *usage,
unsigned int cmd_id)
{ {
struct fx_command_usage_entry *u_cmd = malloc(sizeof *u_cmd); struct fx_command_usage_entry *u_cmd = malloc(sizeof *u_cmd);
if (!u_cmd) { if (!u_cmd) {
@@ -392,7 +408,9 @@ static void prepend_command_name(struct fx_command *cmd, fx_string *out)
} }
static void get_qualified_command_name( static void get_qualified_command_name(
struct fx_command *cmd, const struct fx_arglist *args, fx_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++) { for (unsigned int i = 0; i <= args->list_argv_last_command; i++) {
if (i > 0) { if (i > 0) {
@@ -414,8 +432,10 @@ static void get_qualified_command_name(
} }
static void get_usage_string( static void get_usage_string(
struct fx_command *cmd, struct fx_arglist *args, struct fx_command *cmd,
struct fx_command_usage *usage, fx_string *out) struct fx_arglist *args,
struct fx_command_usage *usage,
fx_string *out)
{ {
get_qualified_command_name(cmd, args, out); get_qualified_command_name(cmd, args, out);
@@ -424,7 +444,9 @@ static void get_usage_string(
struct fx_queue_entry *q_entry = fx_queue_first(&usage->u_parts); struct fx_queue_entry *q_entry = fx_queue_first(&usage->u_parts);
while (q_entry) { while (q_entry) {
struct fx_command_usage_entry *entry = fx_unbox( struct fx_command_usage_entry *entry = fx_unbox(
struct fx_command_usage_entry, q_entry, e_entry); struct fx_command_usage_entry,
q_entry,
e_entry);
if (!entry) { if (!entry) {
break; break;
@@ -436,7 +458,10 @@ static void get_usage_string(
switch (entry->e_type) { switch (entry->e_type) {
case CMD_USAGE_ARG: case CMD_USAGE_ARG:
if (entry->e_arg) { if (entry->e_arg) {
z__fx_get_arg_usage_string(entry->e_arg, false, out); z__fx_get_arg_usage_string(
entry->e_arg,
false,
out);
} else { } else {
fx_string_append_cstr(out, "[[ARGS]"); fx_string_append_cstr(out, "[[ARGS]");
} }
@@ -444,7 +469,9 @@ static void get_usage_string(
case CMD_USAGE_OPT: case CMD_USAGE_OPT:
if (entry->e_opt) { if (entry->e_opt) {
z__fx_get_option_usage_string( z__fx_get_option_usage_string(
entry->e_opt, CMD_STR_DIRECT_USAGE, out); entry->e_opt,
CMD_STR_DIRECT_USAGE,
out);
} else { } else {
fx_string_append_cstr(out, "[[OPTIONS]"); fx_string_append_cstr(out, "[[OPTIONS]");
} }
@@ -473,7 +500,8 @@ static void get_usage_string(
} }
fx_string *z__fx_command_default_usage_string( fx_string *z__fx_command_default_usage_string(
struct fx_command *cmd, struct fx_command_option *with_opt, struct fx_command *cmd,
struct fx_command_option *with_opt,
const struct fx_arglist *args) const struct fx_arglist *args)
{ {
fx_string *str = fx_string_create(); fx_string *str = fx_string_create();
@@ -481,7 +509,10 @@ fx_string *z__fx_command_default_usage_string(
if (with_opt) { if (with_opt) {
fx_string_append_cstr(str, " "); fx_string_append_cstr(str, " ");
z__fx_get_option_usage_string(with_opt, CMD_STR_DIRECT_USAGE, str); z__fx_get_option_usage_string(
with_opt,
CMD_STR_DIRECT_USAGE,
str);
} else if (!fx_queue_empty(&cmd->c_opt)) { } else if (!fx_queue_empty(&cmd->c_opt)) {
fx_string_append_cstr(str, " [OPTIONS]"); fx_string_append_cstr(str, " [OPTIONS]");
} }
@@ -519,7 +550,9 @@ static void get_command_string(struct fx_command *cmd, fx_string *out)
} }
fx_string_append_cstrf( fx_string_append_cstrf(
out, F_GREEN "-%c" F_RESET, cmd->c_short_name); out,
F_GREEN "-%c" F_RESET,
cmd->c_short_name);
r++; r++;
} }
@@ -529,7 +562,9 @@ static void get_command_string(struct fx_command *cmd, fx_string *out)
} }
fx_string_append_cstrf( fx_string_append_cstrf(
out, F_GREEN "--%s" F_RESET, cmd->c_long_name); out,
F_GREEN "--%s" F_RESET,
cmd->c_long_name);
r++; r++;
} }
} }
@@ -544,7 +579,8 @@ static void get_command_description(struct fx_command *cmd, fx_string *out)
* 2) the length of the description string exceeds the remaining line length * 2) the length of the description string exceeds the remaining line length
* (once the usage string has been printed. * (once the usage string has been printed.
* or, * or,
* 3) the length of the description string is more than three terminal lines. * 3) the length of the description string is more than three terminal
* lines.
*/ */
#define description_on_separate_line(opt_len, desc_len) \ #define description_on_separate_line(opt_len, desc_len) \
((opt_len >= newline_threshold \ ((opt_len >= newline_threshold \
@@ -578,12 +614,14 @@ static void print_options_list(struct fx_command *cmd)
z__fx_get_option_usage_string(opt, CMD_STR_COLOUR, opt_str); z__fx_get_option_usage_string(opt, CMD_STR_COLOUR, opt_str);
z__fx_get_option_description(opt, desc_str); z__fx_get_option_description(opt, desc_str);
size_t opt_len = fx_string_get_size( size_t opt_len
opt_str, FX_STRLEN_IGNORE_ESC = fx_string_get_size(
| FX_STRLEN_IGNORE_MOD) opt_str,
FX_STRLEN_IGNORE_ESC | FX_STRLEN_IGNORE_MOD)
+ 4; + 4;
size_t desc_len = fx_string_get_size( size_t desc_len = fx_string_get_size(
desc_str, FX_STRLEN_IGNORE_ESC | FX_STRLEN_IGNORE_MOD); desc_str,
FX_STRLEN_IGNORE_ESC | FX_STRLEN_IGNORE_MOD);
if (description_on_separate_line(opt_len, desc_len)) { if (description_on_separate_line(opt_len, desc_len)) {
goto skip; goto skip;
@@ -616,12 +654,14 @@ static void print_options_list(struct fx_command *cmd)
z__fx_get_option_usage_string(opt, CMD_STR_COLOUR, opt_str); z__fx_get_option_usage_string(opt, CMD_STR_COLOUR, opt_str);
z__fx_get_option_description(opt, desc_str); z__fx_get_option_description(opt, desc_str);
size_t opt_len = fx_string_get_size( size_t opt_len
opt_str, FX_STRLEN_IGNORE_ESC = fx_string_get_size(
| FX_STRLEN_IGNORE_MOD) opt_str,
FX_STRLEN_IGNORE_ESC | FX_STRLEN_IGNORE_MOD)
+ 4; + 4;
size_t desc_len = fx_string_get_size( size_t desc_len = fx_string_get_size(
desc_str, FX_STRLEN_IGNORE_ESC | FX_STRLEN_IGNORE_MOD); desc_str,
FX_STRLEN_IGNORE_ESC | FX_STRLEN_IGNORE_MOD);
bool new_paragraph bool new_paragraph
= description_on_separate_line(opt_len, desc_len); = description_on_separate_line(opt_len, desc_len);
@@ -650,7 +690,10 @@ static void print_options_list(struct fx_command *cmd)
len++; len++;
} }
fx_print_paragraph(fx_string_get_cstr(desc_str), OUTPUT_STREAM, &format); fx_print_paragraph(
fx_string_get_cstr(desc_str),
OUTPUT_STREAM,
&format);
if (new_paragraph) { if (new_paragraph) {
fx_tty_putc(OUTPUT_STREAM, 0, '\n'); fx_tty_putc(OUTPUT_STREAM, 0, '\n');
@@ -678,8 +721,10 @@ static void print_args_list(struct fx_command *cmd)
fx_string_clear(str); fx_string_clear(str);
z__fx_get_arg_usage_string(arg, true, str); z__fx_get_arg_usage_string(arg, true, str);
size_t len = fx_string_get_size( size_t len
str, FX_STRLEN_IGNORE_ESC | FX_STRLEN_IGNORE_MOD) = fx_string_get_size(
str,
FX_STRLEN_IGNORE_ESC | FX_STRLEN_IGNORE_MOD)
+ 4; + 4;
if (len > desc_margin) { if (len > desc_margin) {
@@ -705,9 +750,10 @@ static void print_args_list(struct fx_command *cmd)
fx_tty_puts(OUTPUT_STREAM, 0, " "); fx_tty_puts(OUTPUT_STREAM, 0, " ");
fx_tty_puts(OUTPUT_STREAM, 0, fx_string_get_cstr(str)); fx_tty_puts(OUTPUT_STREAM, 0, fx_string_get_cstr(str));
unsigned int len = fx_string_get_size( unsigned int len
str, FX_STRLEN_IGNORE_ESC = fx_string_get_size(
| FX_STRLEN_IGNORE_MOD) str,
FX_STRLEN_IGNORE_ESC | FX_STRLEN_IGNORE_MOD)
+ 4; + 4;
while (len < format.p_left_margin) { while (len < format.p_left_margin) {
fx_tty_putc(OUTPUT_STREAM, 0, ' '); fx_tty_putc(OUTPUT_STREAM, 0, ' ');
@@ -717,7 +763,10 @@ static void print_args_list(struct fx_command *cmd)
fx_string_clear(str); fx_string_clear(str);
z__fx_get_arg_description(arg, str); z__fx_get_arg_description(arg, str);
fx_print_paragraph(fx_string_get_cstr(str), OUTPUT_STREAM, &format); fx_print_paragraph(
fx_string_get_cstr(str),
OUTPUT_STREAM,
&format);
entry = fx_queue_next(entry); entry = fx_queue_next(entry);
} }
@@ -738,8 +787,10 @@ static void print_commands_list(struct fx_command *cmd)
fx_string_clear(str); fx_string_clear(str);
get_command_string(sub, str); get_command_string(sub, str);
size_t len = fx_string_get_size( size_t len
str, FX_STRLEN_IGNORE_ESC | FX_STRLEN_IGNORE_MOD) = fx_string_get_size(
str,
FX_STRLEN_IGNORE_ESC | FX_STRLEN_IGNORE_MOD)
+ 4; + 4;
if (len > desc_margin) { if (len > desc_margin) {
@@ -765,9 +816,10 @@ static void print_commands_list(struct fx_command *cmd)
fx_tty_puts(OUTPUT_STREAM, 0, " "); fx_tty_puts(OUTPUT_STREAM, 0, " ");
fx_tty_puts(OUTPUT_STREAM, 0, fx_string_get_cstr(str)); fx_tty_puts(OUTPUT_STREAM, 0, fx_string_get_cstr(str));
unsigned int len = fx_string_get_size( unsigned int len
str, FX_STRLEN_IGNORE_ESC = fx_string_get_size(
| FX_STRLEN_IGNORE_MOD) str,
FX_STRLEN_IGNORE_ESC | FX_STRLEN_IGNORE_MOD)
+ 4; + 4;
while (len < format.p_left_margin) { while (len < format.p_left_margin) {
fx_tty_putc(OUTPUT_STREAM, 0, ' '); fx_tty_putc(OUTPUT_STREAM, 0, ' ');
@@ -777,7 +829,10 @@ static void print_commands_list(struct fx_command *cmd)
fx_string_clear(str); fx_string_clear(str);
get_command_description(sub, str); get_command_description(sub, str);
fx_print_paragraph(fx_string_get_cstr(str), OUTPUT_STREAM, &format); fx_print_paragraph(
fx_string_get_cstr(str),
OUTPUT_STREAM,
&format);
entry = fx_queue_next(entry); entry = fx_queue_next(entry);
} }
@@ -786,7 +841,8 @@ static void print_commands_list(struct fx_command *cmd)
} }
struct fx_command *fx_command_get_subcommand_with_name( struct fx_command *fx_command_get_subcommand_with_name(
struct fx_command *cmd, const char *name) struct fx_command *cmd,
const char *name)
{ {
struct fx_queue_entry *entry = fx_queue_first(&cmd->c_subcommands); struct fx_queue_entry *entry = fx_queue_first(&cmd->c_subcommands);
while (entry) { while (entry) {
@@ -808,7 +864,8 @@ struct fx_command *fx_command_get_subcommand_with_name(
} }
struct fx_command *fx_command_get_subcommand_with_long_name( struct fx_command *fx_command_get_subcommand_with_long_name(
struct fx_command *cmd, const char *long_name) struct fx_command *cmd,
const char *long_name)
{ {
struct fx_queue_entry *entry = fx_queue_first(&cmd->c_subcommands); struct fx_queue_entry *entry = fx_queue_first(&cmd->c_subcommands);
while (entry) { while (entry) {
@@ -830,7 +887,8 @@ struct fx_command *fx_command_get_subcommand_with_long_name(
} }
struct fx_command *fx_command_get_subcommand_with_short_name( struct fx_command *fx_command_get_subcommand_with_short_name(
struct fx_command *cmd, char short_name) struct fx_command *cmd,
char short_name)
{ {
struct fx_queue_entry *entry = fx_queue_first(&cmd->c_subcommands); struct fx_queue_entry *entry = fx_queue_first(&cmd->c_subcommands);
while (entry) { while (entry) {
@@ -852,7 +910,8 @@ struct fx_command *fx_command_get_subcommand_with_short_name(
} }
struct fx_command_option *fx_command_get_option_with_long_name( struct fx_command_option *fx_command_get_option_with_long_name(
struct fx_command *cmd, const char *long_name) struct fx_command *cmd,
const char *long_name)
{ {
struct fx_queue_entry *entry = fx_queue_first(&cmd->c_opt); struct fx_queue_entry *entry = fx_queue_first(&cmd->c_opt);
while (entry) { while (entry) {
@@ -874,7 +933,8 @@ struct fx_command_option *fx_command_get_option_with_long_name(
} }
struct fx_command_option *fx_command_get_option_with_short_name( struct fx_command_option *fx_command_get_option_with_short_name(
struct fx_command *cmd, char short_name) struct fx_command *cmd,
char short_name)
{ {
struct fx_queue_entry *entry = fx_queue_first(&cmd->c_opt); struct fx_queue_entry *entry = fx_queue_first(&cmd->c_opt);
while (entry) { while (entry) {
@@ -896,7 +956,8 @@ struct fx_command_option *fx_command_get_option_with_short_name(
} }
struct fx_command_option *fx_command_get_option_with_id( struct fx_command_option *fx_command_get_option_with_id(
struct fx_command *cmd, unsigned int id) struct fx_command *cmd,
unsigned int id)
{ {
struct fx_queue_entry *entry = fx_queue_first(&cmd->c_opt); struct fx_queue_entry *entry = fx_queue_first(&cmd->c_opt);
while (entry) { while (entry) {
@@ -914,7 +975,8 @@ struct fx_command_option *fx_command_get_option_with_id(
} }
struct fx_command_arg *fx_command_get_arg_with_id( struct fx_command_arg *fx_command_get_arg_with_id(
struct fx_command *cmd, unsigned int id) struct fx_command *cmd,
unsigned int id)
{ {
struct fx_queue_entry *entry = fx_queue_first(&cmd->c_arg); struct fx_queue_entry *entry = fx_queue_first(&cmd->c_arg);
while (entry) { while (entry) {
@@ -941,7 +1003,10 @@ static void print_usage(struct fx_command *cmd, struct fx_arglist *args)
if (fx_queue_empty(&cmd->c_usage)) { if (fx_queue_empty(&cmd->c_usage)) {
fx_string *usage fx_string *usage
= z__fx_command_default_usage_string(cmd, NULL, args); = z__fx_command_default_usage_string(cmd, NULL, args);
fx_print_paragraph(fx_string_get_cstr(usage), OUTPUT_STREAM, &format); fx_print_paragraph(
fx_string_get_cstr(usage),
OUTPUT_STREAM,
&format);
fx_string_unref(usage); fx_string_unref(usage);
return; return;
} }
@@ -954,7 +1019,10 @@ static void print_usage(struct fx_command *cmd, struct fx_arglist *args)
fx_string_clear(str); fx_string_clear(str);
get_usage_string(cmd, args, usage, str); get_usage_string(cmd, args, usage, str);
fx_print_paragraph(fx_string_get_cstr(str), OUTPUT_STREAM, &format); fx_print_paragraph(
fx_string_get_cstr(str),
OUTPUT_STREAM,
&format);
entry = fx_queue_next(entry); entry = fx_queue_next(entry);
} }
@@ -968,7 +1036,10 @@ static void print_help(struct fx_command *cmd, struct fx_arglist *args)
if (!cmd->c_parent) { if (!cmd->c_parent) {
fx_tty_printf( fx_tty_printf(
OUTPUT_STREAM, 0, F_GREEN "%s" F_RESET "\n", cmd->c_name); OUTPUT_STREAM,
0,
F_GREEN "%s" F_RESET "\n",
cmd->c_name);
} }
if (cmd->c_description) { if (cmd->c_description) {
@@ -999,9 +1070,13 @@ static int execute_command(struct fx_command *cmd, struct fx_arglist *args)
} }
size_t nr_items = fx_arglist_get_count( size_t nr_items = fx_arglist_get_count(
args, FX_COMMAND_INVALID_ID, FX_COMMAND_INVALID_ID); args,
FX_COMMAND_INVALID_ID,
FX_COMMAND_INVALID_ID);
size_t nr_help = fx_arglist_get_count( size_t nr_help = fx_arglist_get_count(
args, FX_COMMAND_OPTION_HELP, FX_COMMAND_INVALID_ID); args,
FX_COMMAND_OPTION_HELP,
FX_COMMAND_INVALID_ID);
if ((cmd->c_flags & FX_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); print_help(cmd, args);
@@ -1020,7 +1095,9 @@ static int execute_command(struct fx_command *cmd, struct fx_arglist *args)
return -1; return -1;
} }
static fx_status add_subcommand(struct fx_command *parent, struct fx_command *child) static fx_status add_subcommand(
struct fx_command *parent,
struct fx_command *child)
{ {
fx_queue_push_back(&parent->c_subcommands, &child->c_entry); fx_queue_push_back(&parent->c_subcommands, &child->c_entry);
child->c_parent = parent; child->c_parent = parent;
@@ -1032,7 +1109,8 @@ static int resolve_command_parents(struct fx_bst *commands)
{ {
struct fx_bst_node *node = fx_bst_first(commands); struct fx_bst_node *node = fx_bst_first(commands);
while (node) { while (node) {
struct fx_command *cmd = fx_unbox(struct fx_command, node, c_node); struct fx_command *cmd
= fx_unbox(struct fx_command, node, c_node);
if (cmd->c_parent_id == FX_COMMAND_INVALID_ID) { if (cmd->c_parent_id == FX_COMMAND_INVALID_ID) {
goto skip; goto skip;
+36 -18
View File
@@ -1,10 +1,10 @@
#ifndef _FX_COMMAND_H_ #ifndef _FX_COMMAND_H_
#define _FX_COMMAND_H_ #define _FX_COMMAND_H_
#include <fx/cmd.h> #include <fx/bst.h>
#include <fx/core/bst.h> #include <fx/cmdline/cmd.h>
#include <fx/core/queue.h> #include <fx/queue.h>
#include <fx/ds/string.h> #include <fx/string.h>
#define F_RED "[bright_red]" #define F_RED "[bright_red]"
#define F_GREEN "[bright_green]" #define F_GREEN "[bright_green]"
@@ -105,23 +105,31 @@ struct fx_arglist {
}; };
FX_API struct fx_command *fx_command_get_subcommand_with_name( FX_API struct fx_command *fx_command_get_subcommand_with_name(
struct fx_command *cmd, const char *name); struct fx_command *cmd,
const char *name);
FX_API struct fx_command *fx_command_get_subcommand_with_long_name( FX_API struct fx_command *fx_command_get_subcommand_with_long_name(
struct fx_command *cmd, const char *long_name); struct fx_command *cmd,
const char *long_name);
FX_API struct fx_command *fx_command_get_subcommand_with_short_name( FX_API struct fx_command *fx_command_get_subcommand_with_short_name(
struct fx_command *cmd, char short_name); struct fx_command *cmd,
char short_name);
FX_API struct fx_command_option *fx_command_get_option_with_long_name( FX_API struct fx_command_option *fx_command_get_option_with_long_name(
struct fx_command *cmd, const char *long_name); struct fx_command *cmd,
const char *long_name);
FX_API struct fx_command_option *fx_command_get_option_with_short_name( FX_API struct fx_command_option *fx_command_get_option_with_short_name(
struct fx_command *cmd, char short_name); struct fx_command *cmd,
char short_name);
FX_API struct fx_command_option *fx_command_get_option_with_id( FX_API struct fx_command_option *fx_command_get_option_with_id(
struct fx_command *cmd, unsigned int id); struct fx_command *cmd,
unsigned int id);
FX_API struct fx_command_arg *fx_command_get_arg_with_id( FX_API struct fx_command_arg *fx_command_get_arg_with_id(
struct fx_command *cmd, unsigned int id); struct fx_command *cmd,
unsigned int id);
FX_API struct fx_command_arg *fx_command_option_get_arg_with_id( FX_API struct fx_command_arg *fx_command_option_get_arg_with_id(
struct fx_command_option *opt, unsigned int id); struct fx_command_option *opt,
unsigned int id);
FX_API struct fx_command_option *fx_command_option_create(void); FX_API struct fx_command_option *fx_command_option_create(void);
FX_API void fx_command_option_destroy(struct fx_command_option *opt); FX_API void fx_command_option_destroy(struct fx_command_option *opt);
@@ -131,21 +139,31 @@ FX_API void fx_command_arg_destroy(struct fx_command_arg *arg);
FX_API struct fx_arglist *fx_arglist_create(void); FX_API struct fx_arglist *fx_arglist_create(void);
FX_API fx_status fx_arglist_parse( FX_API fx_status fx_arglist_parse(
struct fx_arglist *args, struct fx_command **cmd, int argc, struct fx_arglist *args,
struct fx_command **cmd,
int argc,
const char **argv); const char **argv);
FX_API void fx_arglist_destroy(struct fx_arglist *args); FX_API void fx_arglist_destroy(struct fx_arglist *args);
FX_API fx_string *z__fx_command_default_usage_string( FX_API fx_string *z__fx_command_default_usage_string(
struct fx_command *cmd, struct fx_command_option *with_opt, struct fx_command *cmd,
struct fx_command_option *with_opt,
const struct fx_arglist *args); const struct fx_arglist *args);
FX_API void z__fx_get_arg_usage_string( FX_API void z__fx_get_arg_usage_string(
struct fx_command_arg *arg, bool colour, fx_string *out); struct fx_command_arg *arg,
FX_API void z__fx_get_arg_description(struct fx_command_arg *arg, fx_string *out); bool colour,
fx_string *out);
FX_API void z__fx_get_arg_description(
struct fx_command_arg *arg,
fx_string *out);
FX_API void z__fx_get_option_usage_string( FX_API void z__fx_get_option_usage_string(
struct fx_command_option *opt, enum cmd_string_flags flags, fx_string *out); struct fx_command_option *opt,
enum cmd_string_flags flags,
fx_string *out);
FX_API void z__fx_get_option_description( FX_API void z__fx_get_option_description(
struct fx_command_option *opt, fx_string *out); struct fx_command_option *opt,
fx_string *out);
#endif #endif
@@ -1,27 +1,34 @@
#ifndef FX_CMD_H_ #ifndef FX_CMD_H_
#define FX_CMD_H_ #define FX_CMD_H_
#include <fx/core/bst.h> #include <fx/bst.h>
#include <fx/core/init.h> #include <fx/collections/array.h>
#include <fx/core/iterator.h> #include <fx/init.h>
#include <fx/core/queue.h> #include <fx/iterator.h>
#include <fx/ds/array.h> #include <fx/queue.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#define fx_arglist_foreach(it, q) \ #define fx_arglist_foreach(it, q) \
for (int z__fx_unique_name() = fx_arglist_iterator_begin( \ for (int z__fx_unique_name() = fx_arglist_iterator_begin( \
q, FX_COMMAND_INVALID_ID, FX_COMMAND_INVALID_ID, (it)); \ q, \
fx_arglist_iterator_is_valid(it); fx_arglist_iterator_next(it)) 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) \ #define fx_arglist_foreach_filtered(it, q, opt_id, arg_id) \
for (int z__fx_unique_name() \ for (int z__fx_unique_name() \
= fx_arglist_iterator_begin(q, opt_id, arg_id, (it)); \ = fx_arglist_iterator_begin(q, opt_id, arg_id, (it)); \
fx_arglist_iterator_is_valid(it); fx_arglist_iterator_next(it)) fx_arglist_iterator_is_valid(it); \
fx_arglist_iterator_next(it))
#define fx_arglist_option_foreach(it, q) \ #define fx_arglist_option_foreach(it, q) \
for (int z__fx_unique_name() \ for (int z__fx_unique_name() = fx_arglist_option_iterator_begin( \
= fx_arglist_option_iterator_begin(q, FX_COMMAND_INVALID_ID, (it)); \ q, \
FX_COMMAND_INVALID_ID, \
(it)); \
fx_arglist_option_iterator_is_valid(it); \ fx_arglist_option_iterator_is_valid(it); \
fx_arglist_option_iterator_next(it)) fx_arglist_option_iterator_next(it))
@@ -34,7 +41,9 @@
#define FX_COMMAND(id, parent_id) \ #define FX_COMMAND(id, parent_id) \
static fx_command *command_##id = NULL; \ static fx_command *command_##id = NULL; \
static void __init_##id( \ static void __init_##id( \
fx_command *, fx_command_option *, fx_command_arg *, \ fx_command *, \
fx_command_option *, \
fx_command_arg *, \
fx_command_usage *); \ fx_command_usage *); \
FX_INIT(init_##id) \ FX_INIT(init_##id) \
{ \ { \
@@ -46,8 +55,10 @@
fx_command_register(command_##id); \ fx_command_register(command_##id); \
} \ } \
static void __init_##id( \ static void __init_##id( \
fx_command *this_cmd, fx_command_option *this_opt, \ fx_command *this_cmd, \
fx_command_arg *this_arg, fx_command_usage *this_usage) 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_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_LONG_NAME(name) fx_command_set_long_name(this_cmd, (name))
@@ -69,9 +80,12 @@
#define FX_COMMAND_HELP_OPTION() \ #define FX_COMMAND_HELP_OPTION() \
do { \ do { \
fx_command_option *opt \ fx_command_option *opt = fx_command_add_option( \
= fx_command_add_option(this_cmd, FX_COMMAND_OPTION_HELP); \ this_cmd, \
fx_command_option_set_description(opt, "Show this help message"); \ 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_short_name(opt, 'h'); \
fx_command_option_set_long_name(opt, "help"); \ fx_command_option_set_long_name(opt, "help"); \
} while (0) } while (0)
@@ -109,7 +123,8 @@
fx_command_arg_set_allowed_values(this_arg, allowed_values) fx_command_arg_set_allowed_values(this_arg, allowed_values)
#define FX_COMMAND_USAGE() \ #define FX_COMMAND_USAGE() \
fx_command_usage *z__fx_unique_name() = fx_command_add_usage(this_cmd); \ fx_command_usage *z__fx_unique_name() \
= fx_command_add_usage(this_cmd); \
this_usage = z__fx_unique_name(); \ this_usage = z__fx_unique_name(); \
if (this_usage) if (this_usage)
@@ -197,90 +212,137 @@ typedef struct fx_arglist fx_arglist;
typedef struct fx_arglist_option fx_arglist_option; typedef struct fx_arglist_option fx_arglist_option;
typedef int (*fx_command_callback)( typedef int (*fx_command_callback)(
const fx_command *, const fx_arglist *, const fx_array *); const fx_command *,
const fx_arglist *,
const fx_array *);
FX_API fx_command *fx_command_create(unsigned int id); FX_API fx_command *fx_command_create(unsigned int id);
FX_API void fx_command_destroy(fx_command *cmd); FX_API void fx_command_destroy(fx_command *cmd);
FX_API fx_status fx_command_register(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 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_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_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_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_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_description(
fx_command *cmd,
const char *description);
FX_API fx_status fx_command_set_callback( FX_API fx_status fx_command_set_callback(
fx_command *cmd, fx_command_callback 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_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_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_arg *fx_command_add_arg(fx_command *cmd, int id);
FX_API fx_command_usage *fx_command_add_usage(fx_command *cmd); 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 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 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 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 const char *fx_command_option_get_description(fx_command_option *opt);
FX_API fx_status fx_command_option_set_long_name( FX_API fx_status fx_command_option_set_long_name(
fx_command_option *opt, const char *name); fx_command_option *opt,
FX_API fx_status fx_command_option_set_short_name(fx_command_option *opt, char name); 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_API fx_status fx_command_option_set_description(
fx_command_option *opt, const char *description); fx_command_option *opt,
FX_API fx_command_arg *fx_command_option_add_arg(fx_command_option *opt, int id); 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_name(fx_command_arg *arg, const char *name);
FX_API fx_status fx_command_arg_set_description( FX_API fx_status fx_command_arg_set_description(
fx_command_arg *arg, const char *description); fx_command_arg *arg,
const char *description);
FX_API fx_status fx_command_arg_set_nr_values( FX_API fx_status fx_command_arg_set_nr_values(
fx_command_arg *arg, fx_command_arg_value_count nr_values); fx_command_arg *arg,
fx_command_arg_value_count nr_values);
FX_API fx_status fx_command_arg_set_allowed_values( FX_API fx_status fx_command_arg_set_allowed_values(
fx_command_arg *arg, const char **allowed_values); fx_command_arg *arg,
const char **allowed_values);
FX_API fx_status fx_command_usage_add_option( FX_API fx_status fx_command_usage_add_option(
fx_command_usage *usage, fx_command_option *opt); fx_command_usage *usage,
fx_command_option *opt);
FX_API fx_status fx_command_usage_add_arg( FX_API fx_status fx_command_usage_add_arg(
fx_command_usage *usage, fx_command_arg *opt); fx_command_usage *usage,
fx_command_arg *opt);
FX_API fx_status fx_command_usage_add_command( FX_API fx_status fx_command_usage_add_command(
fx_command_usage *usage, unsigned int cmd_id); fx_command_usage *usage,
unsigned int cmd_id);
FX_API fx_status fx_arglist_get_string( FX_API fx_status fx_arglist_get_string(
const fx_arglist *args, unsigned int opt_id, unsigned int arg_id, const fx_arglist *args,
unsigned int index, const char **out); unsigned int opt_id,
unsigned int arg_id,
unsigned int index,
const char **out);
FX_API fx_status fx_arglist_get_int( FX_API fx_status fx_arglist_get_int(
const fx_arglist *args, unsigned int opt_id, unsigned int arg_id, const fx_arglist *args,
unsigned int index, long long *out); unsigned int opt_id,
unsigned int arg_id,
unsigned int index,
long long *out);
FX_API fx_status fx_arglist_get_uint( FX_API fx_status fx_arglist_get_uint(
const fx_arglist *args, unsigned int opt_id, unsigned int arg_id, const fx_arglist *args,
unsigned int index, unsigned long long *out); unsigned int opt_id,
unsigned int arg_id,
unsigned int index,
unsigned long long *out);
FX_API fx_status fx_arglist_get_option( FX_API fx_status fx_arglist_get_option(
const fx_arglist *args, unsigned int opt_id, unsigned int index, const fx_arglist *args,
unsigned int opt_id,
unsigned int index,
fx_arglist_option **out); fx_arglist_option **out);
FX_API size_t fx_arglist_get_count( FX_API size_t fx_arglist_get_count(
const fx_arglist *args, unsigned int opt_id, unsigned int arg_id); const fx_arglist *args,
unsigned int opt_id,
unsigned int arg_id);
FX_API fx_status fx_arglist_report_missing_option( FX_API fx_status fx_arglist_report_missing_option(
const fx_arglist *args, unsigned int opt_id); const fx_arglist *args,
unsigned int opt_id);
FX_API fx_status fx_arglist_report_unexpected_arg( FX_API fx_status fx_arglist_report_unexpected_arg(
const fx_arglist *args, const char *value); const fx_arglist *args,
const char *value);
FX_API fx_status fx_arglist_report_invalid_arg_value( FX_API fx_status fx_arglist_report_invalid_arg_value(
const fx_arglist *args, unsigned int opt_id, unsigned int arg_id, const fx_arglist *args,
unsigned int opt_id,
unsigned int arg_id,
const char *value); const char *value);
FX_API fx_status fx_arglist_report_missing_args( FX_API fx_status fx_arglist_report_missing_args(
const fx_arglist *args, unsigned int opt_id, unsigned int arg_id, const fx_arglist *args,
unsigned int opt_id,
unsigned int arg_id,
size_t nr_supplied); size_t nr_supplied);
FX_API fx_status fx_arglist_option_get_value( FX_API fx_status fx_arglist_option_get_value(
const fx_arglist_option *opt, unsigned int arg_id, unsigned int index, const fx_arglist_option *opt,
unsigned int arg_id,
unsigned int index,
fx_arglist_value **out); fx_arglist_value **out);
FX_API int fx_arglist_iterator_begin( FX_API int fx_arglist_iterator_begin(
const fx_arglist *args, unsigned int opt_filter, unsigned int arg_filter, const fx_arglist *args,
unsigned int opt_filter,
unsigned int arg_filter,
fx_arglist_iterator *it); fx_arglist_iterator *it);
FX_API bool fx_arglist_iterator_next(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 bool fx_arglist_iterator_is_valid(const fx_arglist_iterator *it);
FX_API int fx_arglist_option_iterator_begin( FX_API int fx_arglist_option_iterator_begin(
const fx_arglist *args, unsigned int opt_filter, const fx_arglist *args,
unsigned int opt_filter,
fx_arglist_option_iterator *it); 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_next(fx_arglist_option_iterator *it);
FX_API bool fx_arglist_option_iterator_is_valid( FX_API bool fx_arglist_option_iterator_is_valid(
+28 -13
View File
@@ -1,7 +1,7 @@
#include "command.h" #include "command.h"
#include <fx/cmd.h> #include <fx/cmdline/cmd.h>
#include <fx/ds/string.h> #include <fx/string.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@@ -58,7 +58,8 @@ const char *fx_command_option_get_description(struct fx_command_option *opt)
} }
fx_status fx_command_option_set_long_name( fx_status fx_command_option_set_long_name(
struct fx_command_option *opt, const char *name) struct fx_command_option *opt,
const char *name)
{ {
char *n = fx_strdup(name); char *n = fx_strdup(name);
if (!n) { if (!n) {
@@ -74,14 +75,17 @@ fx_status fx_command_option_set_long_name(
return FX_SUCCESS; return FX_SUCCESS;
} }
fx_status fx_command_option_set_short_name(struct fx_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; opt->opt_short_name = name;
return FX_SUCCESS; return FX_SUCCESS;
} }
fx_status fx_command_option_set_description( fx_status fx_command_option_set_description(
struct fx_command_option *opt, const char *description) struct fx_command_option *opt,
const char *description)
{ {
char *desc = fx_strdup(description); char *desc = fx_strdup(description);
if (!desc) { if (!desc) {
@@ -97,7 +101,9 @@ fx_status fx_command_option_set_description(
return FX_SUCCESS; return FX_SUCCESS;
} }
struct fx_command_arg *fx_command_option_add_arg(struct fx_command_option *opt, int id) struct fx_command_arg *fx_command_option_add_arg(
struct fx_command_option *opt,
int id)
{ {
struct fx_command_arg *arg = malloc(sizeof *arg); struct fx_command_arg *arg = malloc(sizeof *arg);
if (!arg) { if (!arg) {
@@ -139,7 +145,9 @@ void z__fx_get_option_description(struct fx_command_option *opt, fx_string *out)
if (nr_args > 1) { if (nr_args > 1) {
fx_string_append_cstrf( fx_string_append_cstrf(
out, "values for `%s`:", arg->arg_name); out,
"values for `%s`:",
arg->arg_name);
} else { } else {
fx_string_append_cstr(out, "values:"); fx_string_append_cstr(out, "values:");
} }
@@ -150,7 +158,8 @@ void z__fx_get_option_description(struct fx_command_option *opt, fx_string *out)
} }
fx_string_append_cstrf( fx_string_append_cstrf(
out, " " F_GREEN "%s" F_RESET, out,
" " F_GREEN "%s" F_RESET,
arg->arg_allowed_values[i]); arg->arg_allowed_values[i]);
} }
@@ -165,7 +174,9 @@ void z__fx_get_option_description(struct fx_command_option *opt, fx_string *out)
} }
void z__fx_get_option_usage_string( void z__fx_get_option_usage_string(
struct fx_command_option *opt, enum cmd_string_flags flags, fx_string *out) struct fx_command_option *opt,
enum cmd_string_flags flags,
fx_string *out)
{ {
if (flags & CMD_STR_DIRECT_USAGE) { if (flags & CMD_STR_DIRECT_USAGE) {
fx_string_append_cstr(out, "{"); fx_string_append_cstr(out, "{");
@@ -174,19 +185,22 @@ void z__fx_get_option_usage_string(
if (opt->opt_short_name) { if (opt->opt_short_name) {
fx_string_append_cstrf( fx_string_append_cstrf(
out, out,
(flags & CMD_STR_COLOUR) ? F_GREEN "-%c" F_RESET : "-%c", (flags & CMD_STR_COLOUR) ? F_GREEN "-%c" F_RESET
: "-%c",
opt->opt_short_name); opt->opt_short_name);
} }
if (opt->opt_short_name && opt->opt_long_name) { if (opt->opt_short_name && opt->opt_long_name) {
fx_string_append_cstr( fx_string_append_cstr(
out, (flags & CMD_STR_DIRECT_USAGE) ? "|" : ", "); out,
(flags & CMD_STR_DIRECT_USAGE) ? "|" : ", ");
} }
if (opt->opt_long_name) { if (opt->opt_long_name) {
fx_string_append_cstrf( fx_string_append_cstrf(
out, out,
(flags & CMD_STR_COLOUR) ? F_GREEN "--%s" F_RESET : "--%s", (flags & CMD_STR_COLOUR) ? F_GREEN "--%s" F_RESET
: "--%s",
opt->opt_long_name); opt->opt_long_name);
} }
@@ -254,7 +268,8 @@ void z__fx_get_option_usage_string(
} }
struct fx_command_arg *fx_command_option_get_arg_with_id( struct fx_command_arg *fx_command_option_get_arg_with_id(
struct fx_command_option *opt, unsigned int id) struct fx_command_option *opt,
unsigned int id)
{ {
struct fx_queue_entry *entry = fx_queue_first(&opt->opt_args); struct fx_queue_entry *entry = fx_queue_first(&opt->opt_args);
+55 -24
View File
@@ -1,13 +1,14 @@
#include "command.h" #include "command.h"
#include <assert.h> #include <assert.h>
#include <fx/core/stringstream.h> #include <fx/string.h>
#include <fx/ds/string.h> #include <fx/stringstream.h>
#include <fx/term/print.h> #include <fx/term/print.h>
#include <stdio.h> #include <stdio.h>
enum fx_status fx_arglist_report_missing_option( enum fx_status fx_arglist_report_missing_option(
const fx_arglist *args, unsigned int opt_id) const fx_arglist *args,
unsigned int opt_id)
{ {
struct fx_command_option *opt = NULL; struct fx_command_option *opt = NULL;
@@ -35,7 +36,10 @@ enum fx_status fx_arglist_report_missing_option(
if (opt_names == 2) { if (opt_names == 2) {
fx_stream_write_fmt( fx_stream_write_fmt(
opt_name, NULL, "-%c / --%s", opt->opt_short_name, opt_name,
NULL,
"-%c / --%s",
opt->opt_short_name,
opt->opt_long_name); opt->opt_long_name);
} else if (opt->opt_short_name) { } else if (opt->opt_short_name) {
fx_stream_write_fmt(opt_name, NULL, "-%c", opt->opt_short_name); fx_stream_write_fmt(opt_name, NULL, "-%c", opt->opt_short_name);
@@ -55,10 +59,13 @@ enum fx_status fx_arglist_report_missing_option(
} }
enum fx_status fx_arglist_report_unexpected_arg( enum fx_status fx_arglist_report_unexpected_arg(
const fx_arglist *args, const char *value) const fx_arglist *args,
const char *value)
{ {
fx_string *usage = z__fx_command_default_usage_string( fx_string *usage = z__fx_command_default_usage_string(
args->list_command, NULL, args); args->list_command,
NULL,
args);
fx_err("unexpected argument '" F_YELLOW "%s" F_RESET "' found.", value); fx_err("unexpected argument '" F_YELLOW "%s" F_RESET "' found.", value);
fx_i("usage: %s", fx_string_get_cstr(usage)); fx_i("usage: %s", fx_string_get_cstr(usage));
@@ -68,7 +75,9 @@ enum fx_status fx_arglist_report_unexpected_arg(
} }
enum fx_status fx_arglist_report_invalid_arg_value( enum fx_status fx_arglist_report_invalid_arg_value(
const fx_arglist *args, unsigned int opt_id, unsigned int arg_id, const fx_arglist *args,
unsigned int opt_id,
unsigned int arg_id,
const char *value) const char *value)
{ {
struct fx_command_option *opt = NULL; struct fx_command_option *opt = NULL;
@@ -80,11 +89,15 @@ enum fx_status fx_arglist_report_invalid_arg_value(
if (arg_id != FX_COMMAND_INVALID_ID) { if (arg_id != FX_COMMAND_INVALID_ID) {
arg = opt ? fx_command_option_get_arg_with_id(opt, arg_id) arg = opt ? fx_command_option_get_arg_with_id(opt, arg_id)
: fx_command_get_arg_with_id(args->list_command, arg_id); : fx_command_get_arg_with_id(
args->list_command,
arg_id);
} }
fx_string *usage = z__fx_command_default_usage_string( fx_string *usage = z__fx_command_default_usage_string(
args->list_command, opt, args); args->list_command,
opt,
args);
fx_string *opt_string = fx_string_create(); fx_string *opt_string = fx_string_create();
if (opt) { if (opt) {
@@ -95,15 +108,18 @@ enum fx_status fx_arglist_report_invalid_arg_value(
fx_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 "'.", "%s" F_RESET "'.",
value, fx_string_get_cstr(opt_string)); value,
fx_string_get_cstr(opt_string));
if (opt) { if (opt) {
fx_i("'" F_YELLOW "%s" F_RESET fx_i("'" F_YELLOW "%s" F_RESET
"' accepts the following values for '" F_YELLOW "%s" F_RESET "' accepts the following values for '" F_YELLOW
"':", "%s" F_RESET "':",
fx_string_get_cstr(opt_string), arg->arg_name); fx_string_get_cstr(opt_string),
arg->arg_name);
} else { } else {
fx_i("'" F_YELLOW "%s" F_RESET "' accepts the following values:", fx_i("'" F_YELLOW "%s" F_RESET
"' accepts the following values:",
fx_string_get_cstr(opt_string)); fx_string_get_cstr(opt_string));
} }
@@ -124,7 +140,9 @@ enum fx_status fx_arglist_report_invalid_arg_value(
} }
enum fx_status fx_arglist_report_missing_args( enum fx_status fx_arglist_report_missing_args(
const fx_arglist *args, unsigned int opt_id, unsigned int arg_id, const fx_arglist *args,
unsigned int opt_id,
unsigned int arg_id,
size_t args_supplied) size_t args_supplied)
{ {
struct fx_command_option *opt = NULL; struct fx_command_option *opt = NULL;
@@ -137,12 +155,16 @@ enum fx_status fx_arglist_report_missing_args(
if (arg_id != FX_COMMAND_INVALID_ID) { if (arg_id != FX_COMMAND_INVALID_ID) {
arg = opt ? fx_command_option_get_arg_with_id(opt, arg_id) arg = opt ? fx_command_option_get_arg_with_id(opt, arg_id)
: fx_command_get_arg_with_id(args->list_command, arg_id); : fx_command_get_arg_with_id(
args->list_command,
arg_id);
assert(arg); assert(arg);
} }
fx_string *usage = z__fx_command_default_usage_string( fx_string *usage = z__fx_command_default_usage_string(
args->list_command, opt, args); args->list_command,
opt,
args);
fx_string *opt_string = fx_string_create(); fx_string *opt_string = fx_string_create();
if (opt) { if (opt) {
@@ -154,16 +176,19 @@ enum fx_status fx_arglist_report_missing_args(
char supplied_arg_str[64]; char supplied_arg_str[64];
if (args_supplied == 0) { if (args_supplied == 0) {
snprintf( snprintf(
supplied_arg_str, sizeof supplied_arg_str, supplied_arg_str,
sizeof supplied_arg_str,
F_RED_BOLD "none" F_RESET " were provided"); F_RED_BOLD "none" F_RESET " were provided");
} else if (args_supplied == 1) { } else if (args_supplied == 1) {
snprintf( snprintf(
supplied_arg_str, sizeof supplied_arg_str, supplied_arg_str,
sizeof supplied_arg_str,
"only " F_YELLOW_BOLD "%zu" F_RESET " was provided", "only " F_YELLOW_BOLD "%zu" F_RESET " was provided",
args_supplied); args_supplied);
} else { } else {
snprintf( snprintf(
supplied_arg_str, sizeof supplied_arg_str, supplied_arg_str,
sizeof supplied_arg_str,
"only " F_YELLOW_BOLD "%zu" F_RESET " were provided", "only " F_YELLOW_BOLD "%zu" F_RESET " were provided",
args_supplied); args_supplied);
} }
@@ -172,19 +197,25 @@ enum fx_status fx_arglist_report_missing_args(
switch (arg->arg_nr_values) { switch (arg->arg_nr_values) {
case FX_ARG_1_OR_MORE_VALUES: case FX_ARG_1_OR_MORE_VALUES:
snprintf( snprintf(
required_arg_count, sizeof required_arg_count, required_arg_count,
sizeof required_arg_count,
"one or more"); "one or more");
break; break;
default: default:
snprintf( snprintf(
required_arg_count, sizeof required_arg_count, "%d", required_arg_count,
sizeof required_arg_count,
"%d",
arg->arg_nr_values); arg->arg_nr_values);
} }
fx_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.", "%s" F_RESET " `" F_YELLOW "%s" F_RESET "` value%s, but %s.",
fx_string_get_cstr(opt_string), required_arg_count, arg->arg_name, fx_string_get_cstr(opt_string),
(arg->arg_nr_values == 1) ? "" : "s", supplied_arg_str); required_arg_count,
arg->arg_name,
(arg->arg_nr_values == 1) ? "" : "s",
supplied_arg_str);
fx_i("usage: %s", fx_string_get_cstr(usage)); fx_i("usage: %s", fx_string_get_cstr(usage));
fx_i("for more information, use '" F_YELLOW "--help" F_RESET "'"); fx_i("for more information, use '" F_YELLOW "--help" F_RESET "'");
@@ -1,4 +1,4 @@
#include <fx/cmd.h> #include <fx/cmdline/cmd.h>
#include <stdio.h> #include <stdio.h>
enum { enum {
@@ -27,16 +27,24 @@ const char *text
"great pleasure."; "great pleasure.";
static int test_command( static int test_command(
const fx_command *self, const fx_arglist *opt, const fx_array *args) const fx_command *self,
const fx_arglist *opt,
const fx_array *args)
{ {
printf("Hello, world!\n"); printf("Hello, world!\n");
fx_arglist_iterator it; fx_arglist_iterator it;
fx_arglist_iterator_begin( fx_arglist_iterator_begin(
opt, FX_COMMAND_INVALID_ID, FX_COMMAND_INVALID_ID, &it); opt,
FX_COMMAND_INVALID_ID,
FX_COMMAND_INVALID_ID,
&it);
while (fx_arglist_iterator_is_valid(&it)) { while (fx_arglist_iterator_is_valid(&it)) {
printf("opt:%u,arg:%u,i:%zu,value: %s\n", it.opt_id, printf("opt:%u,arg:%u,i:%zu,value: %s\n",
it.value->val_id, it.i, it.value->val_str); it.opt_id,
it.value->val_id,
it.i,
it.value->val_str);
fx_arglist_iterator_next(&it); fx_arglist_iterator_next(&it);
} }
+1 -2
View File
@@ -1,2 +1 @@
export_fx_namespace_details( export_fx_namespace_details(fx.collections)
NAME fx.collections)
+102 -124
View File
@@ -2,6 +2,7 @@
#include <fx/iterator.h> #include <fx/iterator.h>
#include <fx/stream.h> #include <fx/stream.h>
#include <fx/string.h> #include <fx/string.h>
#include <fx/value.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@@ -12,17 +13,17 @@ struct fx_array_p {
size_t ar_len; size_t ar_len;
/* maximum number of items that can currently be stored in array */ /* maximum number of items that can currently be stored in array */
size_t ar_cap; size_t ar_cap;
fx_object **ar_data; fx_value *ar_data;
}; };
struct fx_array_iterator_p { struct fx_array_iterator_p {
fx_array *_a; const fx_array *_a;
struct fx_array_p *_a_p; struct fx_array_p *_a_p;
/** The index of the current value */ /** The index of the current value */
size_t i; size_t i;
/** The current value */ /** The current value */
fx_object *value; fx_value *value;
}; };
/*** PRIVATE FUNCTIONS ********************************************************/ /*** PRIVATE FUNCTIONS ********************************************************/
@@ -32,7 +33,7 @@ static fx_status resize_array(struct fx_array_p *array, size_t new_capacity)
if (array->ar_cap < new_capacity) { if (array->ar_cap < new_capacity) {
void *new_data = realloc( void *new_data = realloc(
array->ar_data, array->ar_data,
new_capacity * sizeof(struct fx_dsref *)); new_capacity * sizeof(fx_value));
if (!new_data) { if (!new_data) {
return FX_ERR_NO_MEMORY; return FX_ERR_NO_MEMORY;
} }
@@ -40,12 +41,12 @@ static fx_status resize_array(struct fx_array_p *array, size_t new_capacity)
array->ar_data = new_data; array->ar_data = new_data;
} else { } else {
for (size_t i = new_capacity; i < array->ar_len; i++) { for (size_t i = new_capacity; i < array->ar_len; i++) {
fx_object_unref(array->ar_data[i]); fx_value_unset(&array->ar_data[i]);
} }
void *new_data = realloc( void *new_data = realloc(
array->ar_data, array->ar_data,
new_capacity * sizeof(struct fx_dsref *)); new_capacity * sizeof(fx_value));
if (!new_data) { if (!new_data) {
return FX_ERR_NO_MEMORY; return FX_ERR_NO_MEMORY;
} }
@@ -63,7 +64,7 @@ static fx_status resize_array(struct fx_array_p *array, size_t new_capacity)
static fx_status array_insert( static fx_status array_insert(
struct fx_array_p *array, struct fx_array_p *array,
fx_object *value, fx_value value,
size_t at) size_t at)
{ {
if (at == FX_NPOS) { if (at == FX_NPOS) {
@@ -84,13 +85,13 @@ static fx_status array_insert(
} }
} }
fx_object **src = array->ar_data + at; fx_value *src = array->ar_data + at;
fx_object **dest = src + 1; fx_value *dest = src + 1;
size_t move_len = (array->ar_len - at) * sizeof(struct fx_dsref *); size_t move_len = (array->ar_len - at) * sizeof(fx_value);
memmove(dest, src, move_len); memmove(dest, src, move_len);
array->ar_data[at] = fx_object_ref(value); fx_value_copy(&array->ar_data[at], &value);
array->ar_len++; array->ar_len++;
return FX_SUCCESS; return FX_SUCCESS;
@@ -102,11 +103,11 @@ static fx_status array_remove(struct fx_array_p *array, size_t at)
return FX_ERR_OUT_OF_BOUNDS; return FX_ERR_OUT_OF_BOUNDS;
} }
fx_object **src = array->ar_data + at; fx_value *src = array->ar_data + at;
fx_object **dest = src + 1; fx_value *dest = src + 1;
size_t move_len = array->ar_len * sizeof(struct fx_dsref *); size_t move_len = array->ar_len * sizeof(fx_value);
fx_object_unref(array->ar_data[at]); fx_value_unset(&array->ar_data[at]);
memmove(dest, src, move_len); memmove(dest, src, move_len);
@@ -125,17 +126,17 @@ static fx_status array_remove_back(struct fx_array_p *array)
return array_remove(array, array->ar_len - 1); return array_remove(array, array->ar_len - 1);
} }
static fx_object *array_pop(struct fx_array_p *array, size_t at) static fx_value array_pop(struct fx_array_p *array, size_t at)
{ {
if (at >= array->ar_len) { if (at >= array->ar_len) {
return NULL; return FX_VALUE_EMPTY;
} }
fx_object **src = array->ar_data + at; fx_value *dest = array->ar_data + at;
fx_object **dest = src + 1; fx_value *src = dest + 1;
size_t move_len = array->ar_len * sizeof(struct fx_dsref *); size_t move_len = array->ar_len * sizeof(fx_value);
fx_object *out = array->ar_data[at]; fx_value out = array->ar_data[at];
memmove(dest, src, move_len); memmove(dest, src, move_len);
@@ -144,40 +145,40 @@ static fx_object *array_pop(struct fx_array_p *array, size_t at)
return out; return out;
} }
static fx_object *array_pop_front(struct fx_array_p *array) static fx_value array_pop_front(struct fx_array_p *array)
{ {
return array_pop(array, 0); return array_pop(array, 0);
} }
static fx_object *array_pop_back(struct fx_array_p *array) static fx_value array_pop_back(struct fx_array_p *array)
{ {
return array_pop(array, array->ar_len - 1); return array_pop(array, array->ar_len - 1);
} }
static fx_object *array_at(const struct fx_array_p *array, size_t at) static fx_value *array_get_ref(const struct fx_array_p *array, size_t at)
{ {
if (at >= array->ar_len) { if (at >= array->ar_len) {
return NULL; return NULL;
} }
return array->ar_data[at]; return &array->ar_data[at];
} }
static fx_object *array_get(struct fx_array_p *array, size_t at) static fx_value array_get(struct fx_array_p *array, size_t at)
{ {
if (at >= array->ar_len) { if (at >= array->ar_len) {
return NULL; return FX_VALUE_EMPTY;
} }
return fx_object_ref(array->ar_data[at]); return fx_value_copy_return(array->ar_data[at]);
} }
static size_t array_size(const struct fx_array_p *array) static size_t array_get_size(const struct fx_array_p *array)
{ {
return array->ar_len; return array->ar_len;
} }
static size_t array_capacity(const struct fx_array_p *array) static size_t array_get_capacity(const struct fx_array_p *array)
{ {
return array->ar_cap; return array->ar_cap;
} }
@@ -189,18 +190,16 @@ static void array_clear(struct fx_array_p *array)
} }
for (size_t i = 0; i < array->ar_len; i++) { for (size_t i = 0; i < array->ar_len; i++) {
fx_object_unref(array->ar_data[i]); fx_value_unset(&array->ar_data[i]);
} }
memset(array->ar_data, 0x0, array->ar_cap * sizeof(fx_object *)); memset(array->ar_data, 0x0, array->ar_cap * sizeof(fx_value));
array->ar_len = 0; array->ar_len = 0;
} }
/*** PUBLIC FUNCTIONS *********************************************************/ /*** PUBLIC FUNCTIONS *********************************************************/
fx_array *fx_array_create_with_values( fx_array *fx_array_create_with_values(const fx_value *values, size_t nr_values)
fx_object *const *values,
size_t nr_values)
{ {
fx_array *array = fx_array_create(); fx_array *array = fx_array_create();
if (!array) { if (!array) {
@@ -211,14 +210,14 @@ fx_array *fx_array_create_with_values(
size_t real_nr_values = 0; size_t real_nr_values = 0;
for (size_t i = 0; i < nr_values; i++) { for (size_t i = 0; i < nr_values; i++) {
if (values[i]) { if (fx_value_is_set(&values[i])) {
real_nr_values++; real_nr_values++;
} }
} }
p->ar_len = real_nr_values; p->ar_len = real_nr_values;
p->ar_cap = real_nr_values; p->ar_cap = real_nr_values;
p->ar_data = calloc(real_nr_values, sizeof(struct fx_dsref *)); p->ar_data = calloc(real_nr_values, sizeof(fx_value));
if (!p->ar_data) { if (!p->ar_data) {
fx_array_unref(array); fx_array_unref(array);
return NULL; return NULL;
@@ -226,13 +225,13 @@ fx_array *fx_array_create_with_values(
size_t index = 0; size_t index = 0;
for (size_t i = 0; i < nr_values; i++) { for (size_t i = 0; i < nr_values; i++) {
p->ar_data[index++] = fx_object_ref(values[i]); fx_value_copy(&p->ar_data[index++], &values[i]);
} }
return array; return array;
} }
fx_status fx_array_insert(fx_array *array, fx_object *value, size_t at) fx_status fx_array_insert(fx_array *array, fx_value value, size_t at)
{ {
FX_CLASS_DISPATCH_STATIC(FX_TYPE_ARRAY, array_insert, array, value, at); FX_CLASS_DISPATCH_STATIC(FX_TYPE_ARRAY, array_insert, array, value, at);
} }
@@ -252,39 +251,39 @@ fx_status fx_array_remove_back(fx_array *array)
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_ARRAY, array_remove_back, array); FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_ARRAY, array_remove_back, array);
} }
fx_object *fx_array_pop(fx_array *array, size_t at) fx_value fx_array_pop(fx_array *array, size_t at)
{ {
FX_CLASS_DISPATCH_STATIC(FX_TYPE_ARRAY, array_pop, array, at); FX_CLASS_DISPATCH_STATIC(FX_TYPE_ARRAY, array_pop, array, at);
} }
fx_object *fx_array_pop_front(fx_array *array) fx_value fx_array_pop_front(fx_array *array)
{ {
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_ARRAY, array_pop_front, array); FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_ARRAY, array_pop_front, array);
} }
fx_object *fx_array_pop_back(fx_array *array) fx_value fx_array_pop_back(fx_array *array)
{ {
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_ARRAY, array_pop_back, array); FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_ARRAY, array_pop_back, array);
} }
fx_object *fx_array_at(const fx_array *array, size_t at) fx_value *fx_array_get_ref(const fx_array *array, size_t at)
{ {
FX_CLASS_DISPATCH_STATIC(FX_TYPE_ARRAY, array_at, array, at); FX_CLASS_DISPATCH_STATIC(FX_TYPE_ARRAY, array_get_ref, array, at);
} }
fx_object *fx_array_get(fx_array *array, size_t at) fx_value fx_array_get(const fx_array *array, size_t at)
{ {
FX_CLASS_DISPATCH_STATIC(FX_TYPE_ARRAY, array_get, array, at); FX_CLASS_DISPATCH_STATIC(FX_TYPE_ARRAY, array_get, array, at);
} }
size_t fx_array_size(const fx_array *array) size_t fx_array_get_size(const fx_array *array)
{ {
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_ARRAY, array_size, array); FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_ARRAY, array_get_size, array);
} }
size_t fx_array_capacity(const fx_array *array) size_t fx_array_get_capacity(const fx_array *array)
{ {
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_ARRAY, array_capacity, array); FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_ARRAY, array_get_capacity, array);
} }
void fx_array_clear(fx_array *array) void fx_array_clear(fx_array *array)
@@ -294,12 +293,12 @@ void fx_array_clear(fx_array *array)
/*** PUBLIC ALIAS FUNCTIONS ***************************************************/ /*** PUBLIC ALIAS FUNCTIONS ***************************************************/
fx_status fx_array_append(fx_array *array, fx_object *value) fx_status fx_array_push_back(fx_array *array, fx_value value)
{ {
return fx_array_insert(array, value, FX_NPOS); return fx_array_insert(array, value, FX_NPOS);
} }
fx_status fx_array_prepend(fx_array *array, fx_object *value) fx_status fx_array_push_front(fx_array *array, fx_value value)
{ {
return fx_array_insert(array, value, 0); return fx_array_insert(array, value, 0);
} }
@@ -317,7 +316,7 @@ static void array_fini(fx_object *obj, void *priv)
if (array->ar_data) { if (array->ar_data) {
for (size_t i = 0; i < array->ar_len; i++) { for (size_t i = 0; i < array->ar_len; i++) {
fx_object_unref(array->ar_data[i]); fx_value_unset(&array->ar_data[i]);
} }
free(array->ar_data); free(array->ar_data);
@@ -325,29 +324,34 @@ static void array_fini(fx_object *obj, void *priv)
} }
} }
static void array_to_string(const fx_object *obj, fx_stream *out) static fx_status array_to_string(
const fx_value *obj,
fx_stream *out,
const char *format)
{ {
struct fx_array_p *array = fx_object_get_private(obj, FX_TYPE_ARRAY); struct fx_array_p *array = fx_object_get_private(
obj->v_object,
FX_TYPE_ARRAY);
if (!array->ar_len) { if (!array->ar_len) {
fx_stream_write_cstr(out, "[]", NULL); fx_stream_write_cstr(out, "[]", NULL);
return; return FX_SUCCESS;
} }
fx_stream_write_cstr(out, "[\n", NULL); fx_stream_write_cstr(out, "[\n", NULL);
fx_stream_push_indent(out, 1); fx_stream_push_indent(out, 1);
size_t len = array_size(array); size_t len = array_get_size(array);
for (size_t i = 0; i < array->ar_len; i++) { for (size_t i = 0; i < array->ar_len; i++) {
fx_object *value = array->ar_data[i]; fx_value *value = &array->ar_data[i];
bool is_string = fx_object_is_type(value, FX_TYPE_STRING); bool is_string = fx_value_is_type(value, FX_TYPE_STRING);
if (is_string) { if (is_string) {
fx_stream_write_char(out, '"'); fx_stream_write_char(out, '"');
} }
fx_object_to_string(value, out); fx_value_to_string(value, out, NULL);
if (is_string) { if (is_string) {
fx_stream_write_char(out, '"'); fx_stream_write_char(out, '"');
@@ -362,40 +366,23 @@ static void array_to_string(const fx_object *obj, fx_stream *out)
fx_stream_pop_indent(out); fx_stream_pop_indent(out);
fx_stream_write_char(out, ']'); fx_stream_write_char(out, ']');
return FX_SUCCESS;
} }
/*** ITERATOR FUNCTIONS *******************************************************/ /*** ITERATOR FUNCTIONS *******************************************************/
static fx_iterator *iterable_begin(fx_object *obj) static const fx_iterator *iterable_begin(const fx_object *obj)
{ {
fx_array_iterator *it_obj = fx_object_create(FX_TYPE_ARRAY_ITERATOR); fx_array_iterator *it_obj = fx_object_create(FX_TYPE_ARRAY_ITERATOR);
struct fx_array_iterator_p *it struct fx_array_iterator_p *it = fx_object_get_private(
= fx_object_get_private(it_obj, FX_TYPE_ARRAY_ITERATOR); it_obj,
FX_TYPE_ARRAY_ITERATOR);
it->_a = obj; it->_a = obj;
it->_a_p = fx_object_get_private(obj, FX_TYPE_ARRAY); it->_a_p = fx_object_get_private(obj, FX_TYPE_ARRAY);
it->i = 0; it->i = 0;
if (it->_a_p->ar_len > 0) { if (it->_a_p->ar_len > 0) {
it->value = it->_a_p->ar_data[0]; it->value = &it->_a_p->ar_data[0];
} else {
fx_iterator_set_status(it_obj, FX_ERR_NO_DATA);
it->value = NULL;
}
return it_obj;
}
static const fx_iterator *iterable_cbegin(const fx_object *obj)
{
fx_array_iterator *it_obj = fx_object_create(FX_TYPE_ARRAY_ITERATOR);
struct fx_array_iterator_p *it
= fx_object_get_private(it_obj, FX_TYPE_ARRAY_ITERATOR);
it->_a = (fx_array *)obj;
it->_a_p = fx_object_get_private(obj, FX_TYPE_ARRAY);
it->i = 0;
if (it->_a_p->ar_len > 0) {
it->value = it->_a_p->ar_data[0];
} else { } else {
fx_iterator_set_status(it_obj, FX_ERR_NO_DATA); fx_iterator_set_status(it_obj, FX_ERR_NO_DATA);
it->value = NULL; it->value = NULL;
@@ -406,12 +393,13 @@ static const fx_iterator *iterable_cbegin(const fx_object *obj)
static enum fx_status iterator_move_next(const fx_iterator *obj) static enum fx_status iterator_move_next(const fx_iterator *obj)
{ {
struct fx_array_iterator_p *it struct fx_array_iterator_p *it = fx_object_get_private(
= fx_object_get_private(obj, FX_TYPE_ARRAY_ITERATOR); obj,
FX_TYPE_ARRAY_ITERATOR);
struct fx_array_p *array = it->_a_p; struct fx_array_p *array = it->_a_p;
if (it->value == NULL || it->i >= array->ar_len) { if (it->value == NULL || it->i >= array->ar_len) {
return false; return FX_ERR_NO_DATA;
} }
it->i++; it->i++;
@@ -419,7 +407,7 @@ static enum fx_status iterator_move_next(const fx_iterator *obj)
if (it->i >= array->ar_len) { if (it->i >= array->ar_len) {
it->value = NULL; it->value = NULL;
} else { } else {
it->value = array->ar_data[it->i]; it->value = &array->ar_data[it->i];
} }
return (it->value != NULL) ? FX_SUCCESS : FX_ERR_NO_DATA; return (it->value != NULL) ? FX_SUCCESS : FX_ERR_NO_DATA;
@@ -427,22 +415,19 @@ static enum fx_status iterator_move_next(const fx_iterator *obj)
static enum fx_status iterator_erase(fx_iterator *obj) static enum fx_status iterator_erase(fx_iterator *obj)
{ {
struct fx_array_iterator_p *it struct fx_array_iterator_p *it = fx_object_get_private(
= fx_object_get_private(obj, FX_TYPE_ARRAY_ITERATOR); obj,
FX_TYPE_ARRAY_ITERATOR);
struct fx_array_p *array = it->_a_p; struct fx_array_p *array = it->_a_p;
if (it->i >= array->ar_len) { if (it->i >= array->ar_len) {
return FX_ERR_OUT_OF_BOUNDS; return FX_ERR_OUT_OF_BOUNDS;
} }
if (array->ar_data[it->i] != it->value) {
return FX_ERR_BAD_STATE;
}
array_remove(array, it->i); array_remove(array, it->i);
if (it->i < array->ar_len) { if (it->i < array->ar_len) {
it->value = array->ar_data[it->i]; it->value = &array->ar_data[it->i];
} else { } else {
it->value = NULL; it->value = NULL;
} }
@@ -450,52 +435,46 @@ static enum fx_status iterator_erase(fx_iterator *obj)
return FX_SUCCESS; return FX_SUCCESS;
} }
static fx_iterator_value iterator_get_value(fx_iterator *obj) static const fx_value *iterator_get_value(const fx_iterator *obj)
{ {
struct fx_array_iterator_p *it struct fx_array_iterator_p *it = fx_object_get_private(
= fx_object_get_private(obj, FX_TYPE_ARRAY_ITERATOR); obj,
FX_TYPE_ARRAY_ITERATOR);
struct fx_array_p *array = it->_a_p;
return FX_ITERATOR_VALUE_PTR(it->value); if (it->i >= array->ar_len) {
} return NULL;
}
static const fx_iterator_value iterator_get_cvalue(const fx_iterator *obj) return it->value;
{
struct fx_array_iterator_p *it
= fx_object_get_private(obj, FX_TYPE_ARRAY_ITERATOR);
return FX_ITERATOR_VALUE_CPTR(it->value);
} }
static enum fx_status iterator_is_valid(const fx_iterator *obj) static enum fx_status iterator_is_valid(const fx_iterator *obj)
{ {
struct fx_array_iterator_p *it struct fx_array_iterator_p *it = fx_object_get_private(
= fx_object_get_private(obj, FX_TYPE_ARRAY_ITERATOR); obj,
FX_TYPE_ARRAY_ITERATOR);
struct fx_array_p *array = it->_a_p; struct fx_array_p *array = it->_a_p;
if (it->i >= array->ar_len) { if (it->i >= array->ar_len) {
return false; return false;
} }
if (array->ar_data[it->i] != it->value) {
return false;
}
return (it->value != NULL) ? FX_SUCCESS : FX_ERR_NO_DATA; return (it->value != NULL) ? FX_SUCCESS : FX_ERR_NO_DATA;
} }
/*** CLASS DEFINITION *********************************************************/ /*** CLASS DEFINITION *********************************************************/
// ---- fx_array DEFINITION // ---- fx_array DEFINITION
FX_TYPE_CLASS_DEFINITION_BEGIN(fx_array) FX_TYPE_CLASS_BEGIN(fx_array)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = array_to_string; FX_INTERFACE_ENTRY(to_string) = array_to_string;
FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT) FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_iterable, FX_TYPE_ITERABLE) FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterable, FX_TYPE_ITERABLE)
FX_INTERFACE_ENTRY(it_begin) = iterable_begin; FX_INTERFACE_ENTRY(it_begin) = iterable_begin;
FX_INTERFACE_ENTRY(it_cbegin) = iterable_cbegin; FX_TYPE_VTABLE_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE)
FX_TYPE_CLASS_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE) FX_TYPE_CLASS_END(fx_array)
FX_TYPE_CLASS_DEFINITION_END(fx_array)
FX_TYPE_DEFINITION_BEGIN(fx_array) FX_TYPE_DEFINITION_BEGIN(fx_array)
FX_TYPE_ID(0xe3c46da1, 0x5f37, 0x4e44, 0xb53b, 0xff5a6200191b); FX_TYPE_ID(0xe3c46da1, 0x5f37, 0x4e44, 0xb53b, 0xff5a6200191b);
@@ -507,18 +486,17 @@ FX_TYPE_DEFINITION_BEGIN(fx_array)
FX_TYPE_DEFINITION_END(fx_array) FX_TYPE_DEFINITION_END(fx_array)
// ---- fx_array_iterator DEFINITION // ---- fx_array_iterator DEFINITION
FX_TYPE_CLASS_DEFINITION_BEGIN(fx_array_iterator) FX_TYPE_CLASS_BEGIN(fx_array_iterator)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL; FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT) FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_iterator, FX_TYPE_ITERATOR) FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterator, FX_TYPE_ITERATOR)
FX_INTERFACE_ENTRY(it_move_next) = iterator_move_next; FX_INTERFACE_ENTRY(it_move_next) = iterator_move_next;
FX_INTERFACE_ENTRY(it_erase) = iterator_erase; FX_INTERFACE_ENTRY(it_erase) = iterator_erase;
FX_INTERFACE_ENTRY(it_get_value) = iterator_get_value; FX_INTERFACE_ENTRY(it_get_value) = iterator_get_value;
FX_INTERFACE_ENTRY(it_get_cvalue) = iterator_get_cvalue; FX_TYPE_VTABLE_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR)
FX_TYPE_CLASS_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR) FX_TYPE_CLASS_END(fx_array_iterator)
FX_TYPE_CLASS_DEFINITION_END(fx_array_iterator)
FX_TYPE_DEFINITION_BEGIN(fx_array_iterator) FX_TYPE_DEFINITION_BEGIN(fx_array_iterator)
FX_TYPE_ID(0xe5e9e8b8, 0x14cb, 0x4192, 0x8138, 0xf45238a2ae73); FX_TYPE_ID(0xe5e9e8b8, 0x14cb, 0x4192, 0x8138, 0xf45238a2ae73);
+4 -4
View File
@@ -23,11 +23,11 @@ static void bitbuffer_fini(fx_object *obj, void *priv)
/*** CLASS DEFINITION *********************************************************/ /*** CLASS DEFINITION *********************************************************/
FX_TYPE_CLASS_DEFINITION_BEGIN(fx_bitbuffer) FX_TYPE_CLASS_BEGIN(fx_bitbuffer)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL; FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT) FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_DEFINITION_END(fx_bitbuffer) FX_TYPE_CLASS_END(fx_bitbuffer)
FX_TYPE_DEFINITION_BEGIN(fx_bitbuffer) FX_TYPE_DEFINITION_BEGIN(fx_bitbuffer)
FX_TYPE_ID(0x628e33da, 0x3109, 0x4a5d, 0x98d5, 0xb0e4cb3ccb65); FX_TYPE_ID(0x628e33da, 0x3109, 0x4a5d, 0x98d5, 0xb0e4cb3ccb65);
+14 -7
View File
@@ -1,6 +1,7 @@
#include <fx/bitop.h> #include <fx/bitop.h>
#include <fx/collections/bitmap.h> #include <fx/collections/bitmap.h>
#include <fx/stream.h> #include <fx/stream.h>
#include <fx/value.h>
#include <string.h> #include <string.h>
#define BITS_PER_WORD (8 * sizeof(bitmap_word_t)) #define BITS_PER_WORD (8 * sizeof(bitmap_word_t))
@@ -306,10 +307,14 @@ static void bitmap_fini(fx_object *obj, void *priv)
struct fx_bitmap_p *map = priv; struct fx_bitmap_p *map = priv;
} }
static void bitmap_to_string(const fx_object *obj, fx_stream *out) static fx_status bitmap_to_string(
const fx_value *obj,
fx_stream *out,
const char *format)
{ {
const struct fx_bitmap_p *map const struct fx_bitmap_p *map = fx_object_get_private(
= fx_object_get_private(obj, FX_TYPE_BITMAP); obj->v_object,
FX_TYPE_BITMAP);
unsigned char *bytes = (unsigned char *)map->map_words; unsigned char *bytes = (unsigned char *)map->map_words;
size_t nr_bytes = map->map_nr_words * sizeof(bitmap_word_t); size_t nr_bytes = map->map_nr_words * sizeof(bitmap_word_t);
@@ -339,15 +344,17 @@ static void bitmap_to_string(const fx_object *obj, fx_stream *out)
fx_stream_write_fmt(out, NULL, "%c", (c & mask) ? '1' : '0'); fx_stream_write_fmt(out, NULL, "%c", (c & mask) ? '1' : '0');
} }
return FX_SUCCESS;
} }
/*** CLASS DEFINITION *********************************************************/ /*** CLASS DEFINITION *********************************************************/
FX_TYPE_CLASS_DEFINITION_BEGIN(fx_bitmap) FX_TYPE_CLASS_BEGIN(fx_bitmap)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = bitmap_to_string; FX_INTERFACE_ENTRY(to_string) = bitmap_to_string;
FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT) FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_DEFINITION_END(fx_bitmap) FX_TYPE_CLASS_END(fx_bitmap)
FX_TYPE_DEFINITION_BEGIN(fx_bitmap) FX_TYPE_DEFINITION_BEGIN(fx_bitmap)
FX_TYPE_ID(0xea115cef, 0x8a63, 0x445f, 0x9474, 0xba9309d5dde8); FX_TYPE_ID(0xea115cef, 0x8a63, 0x445f, 0x9474, 0xba9309d5dde8);
+4 -4
View File
@@ -465,11 +465,11 @@ void buffer_fini(fx_object *obj, void *priv)
/*** CLASS DEFINITION *********************************************************/ /*** CLASS DEFINITION *********************************************************/
FX_TYPE_CLASS_DEFINITION_BEGIN(fx_buffer) FX_TYPE_CLASS_BEGIN(fx_buffer)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL; FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT) FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_DEFINITION_END(fx_buffer) FX_TYPE_CLASS_END(fx_buffer)
FX_TYPE_DEFINITION_BEGIN(fx_buffer) FX_TYPE_DEFINITION_BEGIN(fx_buffer)
FX_TYPE_ID(0x323e6858, 0x7a43, 0x4484, 0xa6fb, 0xe3d1e47ae637); FX_TYPE_ID(0x323e6858, 0x7a43, 0x4484, 0xa6fb, 0xe3d1e47ae637);
-746
View File
@@ -1,746 +0,0 @@
#include <fx/collections/dict.h>
#include <fx/status.h>
#include <fx/stream.h>
#include <fx/string.h>
#include <stdbool.h>
#include <stdlib.h>
/*** PRIVATE DATA *************************************************************/
struct fx_dict_bucket_item {
fx_queue_entry bi_entry;
fx_string *bi_str;
fx_object *bi_value;
};
struct fx_dict_bucket {
fx_bst_node bk_node;
uint64_t bk_hash;
fx_queue bk_items;
};
struct fx_dict_p {
fx_bst d_buckets;
};
struct fx_dict_iterator_p {
size_t i;
fx_dict_item item;
fx_dict *_d;
struct fx_dict_p *_d_p;
fx_bst_node *_cbn;
fx_queue_entry *_cqe;
};
/*** MISC FUNCTIONS ***********************************************************/
static FX_BST_DEFINE_SIMPLE_GET(
struct fx_dict_bucket,
uint64_t,
bk_node,
bk_hash,
get_bucket);
static FX_BST_DEFINE_SIMPLE_INSERT(
struct fx_dict_bucket,
bk_node,
bk_hash,
put_bucket);
uint64_t fx_cstr_hash(const char *s)
{
#define FNV1_OFFSET_BASIS 0xcbf29ce484222325
#define FNV1_PRIME 0x100000001b3
uint64_t hash = FNV1_OFFSET_BASIS;
size_t i = 0;
for (i = 0; s[i]; i++) {
hash ^= s[i];
hash *= FNV1_PRIME;
}
return hash;
}
/*** PRIVATE FUNCTIONS ********************************************************/
static struct fx_dict_bucket *create_bucket(void)
{
struct fx_dict_bucket *bucket = malloc(sizeof *bucket);
if (!bucket) {
return NULL;
}
memset(bucket, 0x0, sizeof *bucket);
return bucket;
}
static struct fx_dict_bucket_item *create_bucket_item(void)
{
struct fx_dict_bucket_item *item = malloc(sizeof *item);
if (!item) {
return NULL;
}
memset(item, 0x0, sizeof *item);
return item;
}
static fx_status dict_put(
struct fx_dict_p *dict,
const char *key,
fx_object *value)
{
uint64_t hash = fx_cstr_hash(key);
struct fx_dict_bucket *bucket = get_bucket(&dict->d_buckets, hash);
if (!bucket) {
bucket = create_bucket();
if (!bucket) {
return FX_ERR_NO_MEMORY;
}
bucket->bk_hash = hash;
put_bucket(&dict->d_buckets, bucket);
}
struct fx_dict_bucket_item *item = create_bucket_item();
if (!item) {
return FX_ERR_NO_MEMORY;
}
item->bi_str = fx_string_create_from_cstr(key);
item->bi_value = fx_object_ref(value);
fx_queue_push_back(&bucket->bk_items, &item->bi_entry);
return FX_SUCCESS;
}
static fx_status dict_put_sk(
struct fx_dict_p *dict,
const fx_string *key,
fx_object *value)
{
uint64_t hash = fx_string_hash(key);
struct fx_dict_bucket *bucket = get_bucket(&dict->d_buckets, hash);
if (!bucket) {
bucket = create_bucket();
if (!bucket) {
return FX_ERR_NO_MEMORY;
}
bucket->bk_hash = hash;
put_bucket(&dict->d_buckets, bucket);
}
struct fx_dict_bucket_item *item = create_bucket_item();
if (!item) {
return FX_ERR_NO_MEMORY;
}
item->bi_str = fx_string_duplicate(key);
item->bi_value = fx_object_ref(value);
fx_queue_push_back(&bucket->bk_items, &item->bi_entry);
return FX_SUCCESS;
}
static fx_object *dict_at(const struct fx_dict_p *dict, const char *key)
{
uint64_t hash = fx_cstr_hash(key);
struct fx_dict_bucket *bucket = get_bucket(&dict->d_buckets, hash);
if (!bucket) {
return NULL;
}
struct fx_queue_entry *entry = fx_queue_first(&bucket->bk_items);
while (entry) {
struct fx_dict_bucket_item *item
= fx_unbox(struct fx_dict_bucket_item, entry, bi_entry);
if (!strcmp(fx_string_get_cstr(item->bi_str), key)) {
return item->bi_value;
}
entry = fx_queue_next(entry);
}
return NULL;
}
static fx_object *dict_at_sk(const struct fx_dict_p *dict, const fx_string *key)
{
uint64_t hash = fx_string_hash(key);
struct fx_dict_bucket *bucket = get_bucket(&dict->d_buckets, hash);
if (!bucket) {
return NULL;
}
struct fx_queue_entry *entry = fx_queue_first(&bucket->bk_items);
while (entry) {
struct fx_dict_bucket_item *item
= fx_unbox(struct fx_dict_bucket_item, entry, bi_entry);
if (fx_string_compare(item->bi_str, key)) {
return item->bi_value;
}
entry = fx_queue_next(entry);
}
return NULL;
}
static fx_object *dict_get(struct fx_dict_p *dict, const char *key)
{
fx_object *value = dict_at(dict, key);
if (value) {
fx_object_ref(value);
}
return value;
}
static fx_object *dict_get_sk(struct fx_dict_p *dict, const fx_string *key)
{
fx_object *value = dict_at_sk(dict, key);
if (value) {
fx_object_ref(value);
}
return value;
}
static bool dict_has_key(const struct fx_dict_p *dict, const char *key)
{
return dict_at(dict, key) != NULL;
}
static bool dict_has_skey(const struct fx_dict_p *dict, const fx_string *key)
{
return dict_at_sk(dict, key) != NULL;
}
static size_t dict_get_size(const struct fx_dict_p *dict)
{
size_t count = 0;
#if 0
fx_bst_iterator it1;
fx_queue_iterator it2;
fx_bst_foreach (&it1, &dict->d_buckets) {
struct fx_dict_bucket *bucket
= fx_unbox(struct fx_dict_bucket, it1.node, bk_node);
fx_queue_foreach (&it2, &bucket->bk_items) {
count++;
}
}
#endif
return count;
}
static bool dict_is_empty(const struct fx_dict_p *dict)
{
fx_bst_node *first_node = fx_bst_first(&dict->d_buckets);
struct fx_dict_bucket *first_bucket
= fx_unbox(struct fx_dict_bucket, first_node, bk_node);
if (!first_bucket) {
return true;
}
fx_queue_entry *first_entry = fx_queue_first(&first_bucket->bk_items);
struct fx_dict_bucket_item *first_item
= fx_unbox(struct fx_dict_bucket_item, first_entry, bi_entry);
if (!first_item) {
return true;
}
return false;
}
static bool get_next_node(
struct fx_bst_node *cur_node,
struct fx_queue_entry *cur_entry,
struct fx_bst_node **out_next_node,
struct fx_queue_entry **out_next_entry)
{
struct fx_dict_bucket *cur_bucket
= fx_unbox(struct fx_dict_bucket, cur_node, bk_node);
if (!cur_bucket) {
return false;
}
struct fx_dict_bucket_item *cur_item
= fx_unbox(struct fx_dict_bucket_item, cur_entry, bi_entry);
if (!cur_item) {
return false;
}
struct fx_bst_node *next_node = cur_node;
struct fx_queue_entry *next_entry = fx_queue_next(cur_entry);
if (!next_entry) {
next_node = fx_bst_next(cur_node);
if (!next_node) {
return false;
}
struct fx_dict_bucket *next_bucket
= fx_unbox(struct fx_dict_bucket, next_node, bk_node);
if (!next_bucket) {
return false;
}
next_entry = fx_queue_first(&next_bucket->bk_items);
if (!next_entry) {
return false;
}
}
struct fx_dict_bucket_item *next_item
= fx_unbox(struct fx_dict_bucket_item, next_entry, bi_entry);
if (!next_item) {
return false;
}
*out_next_node = next_node;
*out_next_entry = next_entry;
return true;
}
static fx_status delete_item(
struct fx_dict_p *dict,
struct fx_dict_bucket *bucket,
struct fx_dict_bucket_item *item)
{
fx_queue_delete(&bucket->bk_items, &item->bi_entry);
free(item);
if (fx_queue_empty(&bucket->bk_items)) {
fx_bst_delete(&dict->d_buckets, &bucket->bk_node);
free(bucket);
}
return FX_SUCCESS;
}
/*** PUBLIC FUNCTIONS *********************************************************/
#if 0
fx_dict *fx_dict_create_with_items(const fx_dict_item *items)
{
fx_dict *dict = fx_dict_create();
if (!dict) {
return NULL;
}
struct fx_dict_p *p = fx_object_get_private(dict, FX_TYPE_DICT);
for (size_t i = 0; items[i].key; i++) {
dict_put(p, items[i].key, items[i].value);
}
return dict;
}
#endif
fx_status fx_dict_put(fx_dict *dict, const char *key, fx_object *value)
{
FX_CLASS_DISPATCH_STATIC(FX_TYPE_DICT, dict_put, dict, key, value);
}
fx_status fx_dict_put_sk(fx_dict *dict, const fx_string *key, fx_object *value)
{
FX_CLASS_DISPATCH_STATIC(FX_TYPE_DICT, dict_put_sk, dict, key, value);
}
fx_object *fx_dict_at(const fx_dict *dict, const char *key)
{
FX_CLASS_DISPATCH_STATIC(FX_TYPE_DICT, dict_at, dict, key);
}
fx_object *fx_dict_at_sk(const fx_dict *dict, const fx_string *key)
{
FX_CLASS_DISPATCH_STATIC(FX_TYPE_DICT, dict_at_sk, dict, key);
}
fx_object *fx_dict_get(fx_dict *dict, const char *key)
{
FX_CLASS_DISPATCH_STATIC(FX_TYPE_DICT, dict_get, dict, key);
}
fx_object *fx_dict_get_sk(fx_dict *dict, const fx_string *key)
{
FX_CLASS_DISPATCH_STATIC(FX_TYPE_DICT, dict_get_sk, dict, key);
}
bool fx_dict_has_key(const fx_dict *dict, const char *key)
{
FX_CLASS_DISPATCH_STATIC(FX_TYPE_DICT, dict_has_key, dict, key);
}
bool fx_dict_has_skey(const fx_dict *dict, const fx_string *key)
{
FX_CLASS_DISPATCH_STATIC(FX_TYPE_DICT, dict_has_skey, dict, key);
}
size_t fx_dict_get_size(const fx_dict *dict)
{
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_DICT, dict_get_size, dict);
}
bool fx_dict_is_empty(const fx_dict *dict)
{
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_DICT, dict_is_empty, dict);
}
/*** PUBLIC ALIAS FUNCTIONS ***************************************************/
/*** VIRTUAL FUNCTIONS ********************************************************/
static void dict_init(fx_object *obj, void *priv)
{
struct fx_dict_p *dict = priv;
}
static void dict_fini(fx_object *obj, void *priv)
{
struct fx_dict_p *dict = priv;
struct fx_bst_node *node = fx_bst_first(&dict->d_buckets);
while (node) {
struct fx_dict_bucket *bucket
= fx_unbox(struct fx_dict_bucket, node, bk_node);
struct fx_bst_node *next_node = fx_bst_next(node);
fx_bst_delete(&dict->d_buckets, node);
struct fx_queue_entry *entry
= fx_queue_first(&bucket->bk_items);
while (entry) {
struct fx_dict_bucket_item *item = fx_unbox(
struct fx_dict_bucket_item,
entry,
bi_entry);
struct fx_queue_entry *next_entry
= fx_queue_next(entry);
fx_queue_delete(&bucket->bk_items, entry);
free(item->bi_str);
fx_object_unref(item->bi_value);
free(item);
entry = next_entry;
}
free(bucket);
node = next_node;
}
}
static void dict_to_string(const fx_object *obj, fx_stream *out)
{
struct fx_dict_p *dict = fx_object_get_private(obj, FX_TYPE_DICT);
if (dict_is_empty(dict)) {
fx_stream_write_cstr(out, "{}", NULL);
return;
}
fx_stream_write_cstr(out, "{\n", NULL);
fx_stream_push_indent(out, 1);
size_t len = dict_get_size(dict);
size_t i = 0;
struct fx_bst_node *node = fx_bst_first(&dict->d_buckets);
while (node) {
struct fx_dict_bucket *bucket
= fx_unbox(struct fx_dict_bucket, node, bk_node);
struct fx_queue_entry *entry
= fx_queue_first(&bucket->bk_items);
while (entry) {
struct fx_dict_bucket_item *item = fx_unbox(
struct fx_dict_bucket_item,
entry,
bi_entry);
fx_object_to_string(item->bi_str, out);
fx_stream_write_cstr(out, ": ", NULL);
bool is_string = fx_object_is_type(
item->bi_value,
FX_TYPE_STRING);
if (is_string) {
fx_stream_write_char(out, '"');
}
fx_object_to_string(item->bi_value, out);
if (is_string) {
fx_stream_write_char(out, '"');
}
if (i < len - 1) {
fx_stream_write_cstr(out, ",", NULL);
}
fx_stream_write_char(out, '\n');
entry = fx_queue_next(entry);
i++;
}
node = fx_bst_next(node);
}
fx_stream_pop_indent(out);
fx_stream_write_char(out, '}');
}
/*** ITERATOR FUNCTIONS *******************************************************/
static fx_iterator *iterable_begin(fx_dict *dict)
{
fx_iterator *it_obj = fx_object_create(FX_TYPE_DICT_ITERATOR);
if (!it_obj) {
return NULL;
}
struct fx_dict_iterator_p *it
= fx_object_get_private(it_obj, FX_TYPE_DICT_ITERATOR);
it->i = 0;
it->_d = dict;
it->_d_p = fx_object_get_private(dict, FX_TYPE_DICT);
if (dict_is_empty(it->_d_p)) {
it->item.key = NULL;
it->item.value = NULL;
fx_iterator_set_status(it_obj, FX_ERR_NO_DATA);
return it_obj;
}
fx_bst_node *first_node = fx_bst_first(&it->_d_p->d_buckets);
struct fx_dict_bucket *first_bucket
= fx_unbox(struct fx_dict_bucket, first_node, bk_node);
if (!first_bucket) {
it->item.key = NULL;
it->item.value = NULL;
fx_iterator_set_status(it_obj, FX_ERR_NO_DATA);
return it_obj;
}
fx_queue_entry *first_entry = fx_queue_first(&first_bucket->bk_items);
struct fx_dict_bucket_item *first_item
= fx_unbox(struct fx_dict_bucket_item, first_entry, bi_entry);
if (!first_item) {
it->item.key = NULL;
it->item.value = NULL;
fx_iterator_set_status(it_obj, FX_ERR_NO_DATA);
return it_obj;
}
it->item.key = first_item->bi_str;
it->item.value = first_item->bi_value;
it->_d = dict;
it->_cbn = first_node;
it->_cqe = first_entry;
return it_obj;
}
static const fx_iterator *iterable_cbegin(const fx_dict *dict)
{
fx_iterator *it_obj = fx_object_create(FX_TYPE_DICT_ITERATOR);
if (!it_obj) {
return NULL;
}
struct fx_dict_iterator_p *it
= fx_object_get_private(it_obj, FX_TYPE_DICT_ITERATOR);
it->i = 0;
it->_d = (fx_dict *)dict;
it->_d_p = fx_object_get_private(dict, FX_TYPE_DICT);
if (dict_is_empty(it->_d_p)) {
it->item.key = NULL;
it->item.value = NULL;
fx_iterator_set_status(it_obj, FX_ERR_NO_DATA);
return it_obj;
}
fx_bst_node *first_node = fx_bst_first(&it->_d_p->d_buckets);
struct fx_dict_bucket *first_bucket
= fx_unbox(struct fx_dict_bucket, first_node, bk_node);
if (!first_bucket) {
it->item.key = NULL;
it->item.value = NULL;
fx_iterator_set_status(it_obj, FX_ERR_NO_DATA);
return it_obj;
}
fx_queue_entry *first_entry = fx_queue_first(&first_bucket->bk_items);
struct fx_dict_bucket_item *first_item
= fx_unbox(struct fx_dict_bucket_item, first_entry, bi_entry);
if (!first_item) {
it->item.key = NULL;
it->item.value = NULL;
fx_iterator_set_status(it_obj, FX_ERR_NO_DATA);
return it_obj;
}
it->item.key = first_item->bi_str;
it->item.value = first_item->bi_value;
it->_cbn = first_node;
it->_cqe = first_entry;
return it_obj;
}
static enum fx_status iterator_move_next(const fx_iterator *obj)
{
struct fx_dict_iterator_p *it
= fx_object_get_private(obj, FX_TYPE_DICT_ITERATOR);
struct fx_bst_node *next_node;
struct fx_queue_entry *next_entry;
if (!get_next_node(it->_cbn, it->_cqe, &next_node, &next_entry)) {
it->item.key = NULL;
it->item.value = NULL;
return FX_ERR_NO_DATA;
}
struct fx_dict_bucket_item *next_item
= fx_unbox(struct fx_dict_bucket_item, next_entry, bi_entry);
if (!next_item) {
it->item.key = NULL;
it->item.value = NULL;
return FX_ERR_NO_DATA;
}
it->i++;
it->item.key = next_item->bi_str;
it->item.value = next_item->bi_value;
it->_cbn = next_node;
it->_cqe = next_entry;
return FX_SUCCESS;
}
static enum fx_status iterator_erase(fx_iterator *obj)
{
struct fx_dict_iterator_p *it
= fx_object_get_private(obj, FX_TYPE_DICT_ITERATOR);
if ((it->item.key || it->item.value) && !(it->_cbn && it->_cqe)) {
return FX_ERR_BAD_STATE;
}
if (!it->item.key || !it->_cqe) {
return FX_ERR_NO_DATA;
}
struct fx_bst_node *next_node;
struct fx_queue_entry *next_entry;
if (!get_next_node(it->_cbn, it->_cqe, &next_node, &next_entry)) {
it->item.key = NULL;
it->item.value = NULL;
return FX_ERR_NO_DATA;
}
struct fx_dict_bucket *cur_bucket
= fx_unbox(struct fx_dict_bucket, it->_cbn, bk_node);
struct fx_dict_bucket_item *cur_item
= fx_unbox(struct fx_dict_bucket_item, it->_cqe, bi_entry);
struct fx_dict_bucket_item *next_item
= fx_unbox(struct fx_dict_bucket_item, next_entry, bi_entry);
fx_status status = delete_item(it->_d_p, cur_bucket, cur_item);
if (FX_ERR(status)) {
return status;
}
if (next_item) {
it->item.key = next_item->bi_str;
it->item.value = next_item->bi_value;
it->_cbn = next_node;
it->_cqe = next_entry;
} else {
it->item.key = NULL;
it->item.value = NULL;
it->_cbn = NULL;
it->_cqe = NULL;
}
return FX_SUCCESS;
}
static fx_iterator_value iterator_get_value(fx_iterator *obj)
{
struct fx_dict_iterator_p *it
= fx_object_get_private(obj, FX_TYPE_DICT_ITERATOR);
return FX_ITERATOR_VALUE_PTR(&it->item);
}
static const fx_iterator_value iterator_get_cvalue(const fx_iterator *obj)
{
struct fx_dict_iterator_p *it
= fx_object_get_private(obj, FX_TYPE_DICT_ITERATOR);
return FX_ITERATOR_VALUE_CPTR(&it->item);
}
/*** CLASS DEFINITION *********************************************************/
// ---- fx_dict DEFINITION
FX_TYPE_CLASS_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)
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)
FX_TYPE_DEFINITION_BEGIN(fx_dict)
FX_TYPE_ID(0xd2af61d9, 0xd0be, 0x4960, 0xbe3f, 0x509749814c10);
FX_TYPE_CLASS(fx_dict_class);
FX_TYPE_IMPLEMENTS(FX_TYPE_ITERABLE);
FX_TYPE_INSTANCE_PRIVATE(struct fx_dict_p);
FX_TYPE_INSTANCE_INIT(dict_init);
FX_TYPE_INSTANCE_FINI(dict_fini);
FX_TYPE_DEFINITION_END(fx_dict)
// ---- fx_dict_iterator DEFINITION
FX_TYPE_CLASS_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)
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)
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)
+88 -67
View File
@@ -32,8 +32,9 @@ struct fx_hashmap_p {
struct fx_hashmap_iterator_p { struct fx_hashmap_iterator_p {
size_t i; size_t i;
fx_hashmap_item item; fx_hashmap_item item;
fx_value item_value;
fx_hashmap *_h; const fx_hashmap *_h;
struct fx_hashmap_p *_h_p; struct fx_hashmap_p *_h_p;
fx_bst_node *_cbn; fx_bst_node *_cbn;
fx_queue_entry *_cqe; fx_queue_entry *_cqe;
@@ -112,14 +113,18 @@ static bool get_next_node(
struct fx_bst_node **out_next_node, struct fx_bst_node **out_next_node,
struct fx_queue_entry **out_next_entry) struct fx_queue_entry **out_next_entry)
{ {
struct fx_hashmap_bucket *cur_bucket struct fx_hashmap_bucket *cur_bucket = fx_unbox(
= fx_unbox(struct fx_hashmap_bucket, cur_node, bk_node); struct fx_hashmap_bucket,
cur_node,
bk_node);
if (!cur_bucket) { if (!cur_bucket) {
return false; return false;
} }
struct fx_hashmap_bucket_item *cur_item struct fx_hashmap_bucket_item *cur_item = fx_unbox(
= fx_unbox(struct fx_hashmap_bucket_item, cur_entry, bi_entry); struct fx_hashmap_bucket_item,
cur_entry,
bi_entry);
if (!cur_item) { if (!cur_item) {
return false; return false;
} }
@@ -146,8 +151,10 @@ static bool get_next_node(
} }
} }
struct fx_hashmap_bucket_item *next_item struct fx_hashmap_bucket_item *next_item = fx_unbox(
= fx_unbox(struct fx_hashmap_bucket_item, next_entry, bi_entry); struct fx_hashmap_bucket_item,
next_entry,
bi_entry);
if (!next_item) { if (!next_item) {
return false; return false;
} }
@@ -186,8 +193,9 @@ static fx_status hashmap_put(
const fx_hashmap_value *value) const fx_hashmap_value *value)
{ {
uint64_t hash = hash_key(key); uint64_t hash = hash_key(key);
struct fx_hashmap_bucket *bucket struct fx_hashmap_bucket *bucket = get_bucket(
= get_bucket(&hashmap->h_buckets, hash); &hashmap->h_buckets,
hash);
if (!bucket) { if (!bucket) {
bucket = create_bucket(); bucket = create_bucket();
@@ -234,8 +242,9 @@ static const struct fx_hashmap_value *hashmap_get(
{ {
uint64_t hash = hash_key(key); uint64_t hash = hash_key(key);
struct fx_hashmap_bucket *bucket struct fx_hashmap_bucket *bucket = get_bucket(
= get_bucket(&hashmap->h_buckets, hash); &hashmap->h_buckets,
hash);
if (!bucket) { if (!bucket) {
return NULL; return NULL;
} }
@@ -262,8 +271,9 @@ static bool hashmap_has_key(
const fx_hashmap_key *key) const fx_hashmap_key *key)
{ {
uint64_t hash = hash_key(key); uint64_t hash = hash_key(key);
struct fx_hashmap_bucket *bucket struct fx_hashmap_bucket *bucket = get_bucket(
= get_bucket(&hashmap->h_buckets, hash); &hashmap->h_buckets,
hash);
if (!bucket) { if (!bucket) {
return false; return false;
} }
@@ -293,8 +303,10 @@ static size_t hashmap_get_size(const struct fx_hashmap_p *hashmap)
static bool hashmap_is_empty(const struct fx_hashmap_p *hashmap) static bool hashmap_is_empty(const struct fx_hashmap_p *hashmap)
{ {
fx_bst_node *first_node = fx_bst_first(&hashmap->h_buckets); fx_bst_node *first_node = fx_bst_first(&hashmap->h_buckets);
struct fx_hashmap_bucket *first_bucket struct fx_hashmap_bucket *first_bucket = fx_unbox(
= fx_unbox(struct fx_hashmap_bucket, first_node, bk_node); struct fx_hashmap_bucket,
first_node,
bk_node);
if (!first_bucket) { if (!first_bucket) {
return true; return true;
} }
@@ -358,8 +370,9 @@ fx_hashmap *fx_hashmap_create_with_items(const fx_hashmap_item *items)
return NULL; return NULL;
} }
struct fx_hashmap_p *p struct fx_hashmap_p *p = fx_object_get_private(
= fx_object_get_private(hashmap, FX_TYPE_HASHMAP); hashmap,
FX_TYPE_HASHMAP);
for (size_t i = 0; items[i].key.key_data && items[i].key.key_size; for (size_t i = 0; items[i].key.key_data && items[i].key.key_size;
i++) { i++) {
@@ -408,12 +421,13 @@ bool fx_hashmap_is_empty(const fx_hashmap *hashmap)
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_HASHMAP, hashmap_is_empty, hashmap); FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_HASHMAP, hashmap_is_empty, hashmap);
} }
fx_iterator *fx_hashmap_begin(fx_hashmap *hashmap) const fx_iterator *fx_hashmap_begin(const fx_hashmap *hashmap)
{ {
fx_hashmap_iterator *it_obj fx_hashmap_iterator *it_obj = fx_object_create(
= fx_object_create(FX_TYPE_HASHMAP_ITERATOR); FX_TYPE_HASHMAP_ITERATOR);
struct fx_hashmap_iterator_p *it struct fx_hashmap_iterator_p *it = fx_object_get_private(
= fx_object_get_private(it_obj, FX_TYPE_HASHMAP_ITERATOR); it_obj,
FX_TYPE_HASHMAP_ITERATOR);
it->_h = hashmap; it->_h = hashmap;
it->_h_p = fx_object_get_private(hashmap, FX_TYPE_HASHMAP); it->_h_p = fx_object_get_private(hashmap, FX_TYPE_HASHMAP);
@@ -426,16 +440,18 @@ fx_iterator *fx_hashmap_begin(fx_hashmap *hashmap)
} }
struct fx_bst_node *first_node = fx_bst_first(&it->_h_p->h_buckets); struct fx_bst_node *first_node = fx_bst_first(&it->_h_p->h_buckets);
struct fx_hashmap_bucket *first_bucket struct fx_hashmap_bucket *first_bucket = fx_unbox(
= fx_unbox(struct fx_hashmap_bucket, first_node, bk_node); struct fx_hashmap_bucket,
first_node,
bk_node);
if (!first_bucket) { if (!first_bucket) {
memset(&it->item, 0x0, sizeof it->item); memset(&it->item, 0x0, sizeof it->item);
fx_iterator_set_status(it_obj, FX_ERR_NO_DATA); fx_iterator_set_status(it_obj, FX_ERR_NO_DATA);
return it_obj; return it_obj;
} }
struct fx_queue_entry *first_entry struct fx_queue_entry *first_entry = fx_queue_first(
= fx_queue_first(&first_bucket->bk_items); &first_bucket->bk_items);
struct fx_hashmap_bucket_item *first_item = fx_unbox( struct fx_hashmap_bucket_item *first_item = fx_unbox(
struct fx_hashmap_bucket_item, struct fx_hashmap_bucket_item,
first_entry, first_entry,
@@ -473,8 +489,10 @@ static void hashmap_fini(fx_object *obj, void *priv)
struct fx_bst_node *node = fx_bst_first(&map->h_buckets); struct fx_bst_node *node = fx_bst_first(&map->h_buckets);
while (node) { while (node) {
struct fx_hashmap_bucket *b struct fx_hashmap_bucket *b = fx_unbox(
= fx_unbox(struct fx_hashmap_bucket, node, bk_node); struct fx_hashmap_bucket,
node,
bk_node);
struct fx_bst_node *next_node = fx_bst_next(node); struct fx_bst_node *next_node = fx_bst_next(node);
fx_bst_delete(&map->h_buckets, node); fx_bst_delete(&map->h_buckets, node);
@@ -484,8 +502,8 @@ static void hashmap_fini(fx_object *obj, void *priv)
struct fx_hashmap_bucket_item, struct fx_hashmap_bucket_item,
entry, entry,
bi_entry); bi_entry);
struct fx_queue_entry *next_entry struct fx_queue_entry *next_entry = fx_queue_next(
= fx_queue_next(entry); entry);
fx_queue_delete(&b->bk_items, entry); fx_queue_delete(&b->bk_items, entry);
if (map->h_key_dtor) { if (map->h_key_dtor) {
@@ -510,8 +528,9 @@ static void hashmap_fini(fx_object *obj, void *priv)
static enum fx_status iterator_move_next(const fx_iterator *obj) static enum fx_status iterator_move_next(const fx_iterator *obj)
{ {
struct fx_hashmap_iterator_p *it struct fx_hashmap_iterator_p *it = fx_object_get_private(
= fx_object_get_private(obj, FX_TYPE_HASHMAP_ITERATOR); obj,
FX_TYPE_HASHMAP_ITERATOR);
struct fx_bst_node *next_node; struct fx_bst_node *next_node;
struct fx_queue_entry *next_entry; struct fx_queue_entry *next_entry;
@@ -520,8 +539,10 @@ static enum fx_status iterator_move_next(const fx_iterator *obj)
return FX_ERR_NO_DATA; return FX_ERR_NO_DATA;
} }
struct fx_hashmap_bucket_item *next_item struct fx_hashmap_bucket_item *next_item = fx_unbox(
= fx_unbox(struct fx_hashmap_bucket_item, next_entry, bi_entry); struct fx_hashmap_bucket_item,
next_entry,
bi_entry);
if (!next_item) { if (!next_item) {
memset(&it->item, 0x0, sizeof it->item); memset(&it->item, 0x0, sizeof it->item);
@@ -540,8 +561,9 @@ static enum fx_status iterator_move_next(const fx_iterator *obj)
static enum fx_status iterator_erase(fx_iterator *obj) static enum fx_status iterator_erase(fx_iterator *obj)
{ {
struct fx_hashmap_iterator_p *it struct fx_hashmap_iterator_p *it = fx_object_get_private(
= fx_object_get_private(obj, FX_TYPE_HASHMAP_ITERATOR); obj,
FX_TYPE_HASHMAP_ITERATOR);
if ((it->item.key.key_data || it->item.value.value_data) if ((it->item.key.key_data || it->item.value.value_data)
&& !(it->_cbn && it->_cqe)) { && !(it->_cbn && it->_cqe)) {
@@ -559,13 +581,19 @@ static enum fx_status iterator_erase(fx_iterator *obj)
return FX_ERR_NO_DATA; return FX_ERR_NO_DATA;
} }
struct fx_hashmap_bucket *cur_bucket struct fx_hashmap_bucket *cur_bucket = fx_unbox(
= fx_unbox(struct fx_hashmap_bucket, it->_cbn, bk_node); struct fx_hashmap_bucket,
struct fx_hashmap_bucket_item *cur_item it->_cbn,
= fx_unbox(struct fx_hashmap_bucket_item, it->_cqe, bi_entry); bk_node);
struct fx_hashmap_bucket_item *cur_item = fx_unbox(
struct fx_hashmap_bucket_item,
it->_cqe,
bi_entry);
struct fx_hashmap_bucket_item *next_item struct fx_hashmap_bucket_item *next_item = fx_unbox(
= fx_unbox(struct fx_hashmap_bucket_item, next_entry, bi_entry); struct fx_hashmap_bucket_item,
next_entry,
bi_entry);
fx_status status = delete_item(it->_h_p, cur_bucket, cur_item); fx_status status = delete_item(it->_h_p, cur_bucket, cur_item);
if (FX_ERR(status)) { if (FX_ERR(status)) {
@@ -590,33 +618,27 @@ static enum fx_status iterator_erase(fx_iterator *obj)
return FX_SUCCESS; return FX_SUCCESS;
} }
static fx_iterator_value iterator_get_value(fx_iterator *obj) static const fx_value *iterator_get_value(const fx_iterator *obj)
{ {
struct fx_hashmap_iterator_p *it struct fx_hashmap_iterator_p *it = fx_object_get_private(
= fx_object_get_private(obj, FX_TYPE_HASHMAP_ITERATOR); obj,
return FX_ITERATOR_VALUE_PTR(&it->item); FX_TYPE_HASHMAP_ITERATOR);
} it->item_value = FX_POINTER(&it->item);
return &it->item_value;
static const fx_iterator_value iterator_get_cvalue(const fx_iterator *obj)
{
const struct fx_hashmap_iterator_p *it
= fx_object_get_private(obj, FX_TYPE_HASHMAP_ITERATOR);
return FX_ITERATOR_VALUE_CPTR(&it->item);
} }
/*** CLASS DEFINITION *********************************************************/ /*** CLASS DEFINITION *********************************************************/
// ---- fx_hashmap DEFINITION // ---- fx_hashmap DEFINITION
FX_TYPE_CLASS_DEFINITION_BEGIN(fx_hashmap) FX_TYPE_CLASS_BEGIN(fx_hashmap)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL; FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT) FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_iterable, FX_TYPE_ITERABLE) FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterable, FX_TYPE_ITERABLE)
FX_INTERFACE_ENTRY(it_begin) = fx_hashmap_begin; FX_INTERFACE_ENTRY(it_begin) = fx_hashmap_begin;
FX_INTERFACE_ENTRY(it_cbegin) = fx_hashmap_cbegin; FX_TYPE_VTABLE_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE)
FX_TYPE_CLASS_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE) FX_TYPE_CLASS_END(fx_hashmap)
FX_TYPE_CLASS_DEFINITION_END(fx_hashmap)
FX_TYPE_DEFINITION_BEGIN(fx_hashmap) FX_TYPE_DEFINITION_BEGIN(fx_hashmap)
FX_TYPE_ID(0x7bf5bcd1, 0x1ff3, 0x4e43, 0xbed8, 0x7c74f28348bf); FX_TYPE_ID(0x7bf5bcd1, 0x1ff3, 0x4e43, 0xbed8, 0x7c74f28348bf);
@@ -628,18 +650,17 @@ FX_TYPE_DEFINITION_BEGIN(fx_hashmap)
FX_TYPE_DEFINITION_END(fx_hashmap) FX_TYPE_DEFINITION_END(fx_hashmap)
// ---- fx_hashmap_iterator DEFINITION // ---- fx_hashmap_iterator DEFINITION
FX_TYPE_CLASS_DEFINITION_BEGIN(fx_hashmap_iterator) FX_TYPE_CLASS_BEGIN(fx_hashmap_iterator)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL; FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT) FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_iterator, FX_TYPE_ITERATOR) FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterator, FX_TYPE_ITERATOR)
FX_INTERFACE_ENTRY(it_move_next) = iterator_move_next; FX_INTERFACE_ENTRY(it_move_next) = iterator_move_next;
FX_INTERFACE_ENTRY(it_erase) = iterator_erase; FX_INTERFACE_ENTRY(it_erase) = iterator_erase;
FX_INTERFACE_ENTRY(it_get_value) = iterator_get_value; FX_INTERFACE_ENTRY(it_get_value) = iterator_get_value;
FX_INTERFACE_ENTRY(it_get_cvalue) = iterator_get_cvalue; FX_TYPE_VTABLE_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR)
FX_TYPE_CLASS_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR) FX_TYPE_CLASS_END(fx_hashmap_iterator)
FX_TYPE_CLASS_DEFINITION_END(fx_hashmap_iterator)
FX_TYPE_DEFINITION_BEGIN(fx_hashmap_iterator) FX_TYPE_DEFINITION_BEGIN(fx_hashmap_iterator)
FX_TYPE_ID(0xd9658456, 0xdd80, 0x419a, 0xb23a, 0xb513013e6431); FX_TYPE_ID(0xd9658456, 0xdd80, 0x419a, 0xb23a, 0xb513013e6431);
+695
View File
@@ -0,0 +1,695 @@
#include <assert.h>
#include <fx/collections/hashtable.h>
#include <fx/misc.h>
#include <fx/reflection/property.h>
#include <fx/status.h>
#include <fx/string.h>
#include <fx/vector.h>
#include <stdbool.h>
#include <stdlib.h>
/*** PRIVATE DATA *************************************************************/
/* these primes are used as array capacities to avoid hash collisions. */
static const size_t primes[] = {
11,
23,
47,
97,
197,
397,
797,
1597,
3203,
6421,
12853,
25717,
51437,
};
struct table_item {
fx_hashtable_item *i_item;
};
struct fx_hashtable_item_p {
fx_hashtable_item *i_self;
fx_value i_key, i_value;
};
struct fx_hashtable_p {
struct table_item *t_items;
/* number of items currently stored in t_items */
size_t t_count;
/* index in primes[] that represents the current maximum size of
* t_items */
size_t t_max_index;
};
struct fx_hashtable_iterator_p {
size_t i;
fx_hashtable_item *item;
fx_value item_value;
const fx_hashtable *_h;
struct fx_hashtable_p *_h_p;
};
/*** PRIVATE FUNCTIONS ********************************************************/
static fx_status convert_key_to_index(
const fx_value *v,
struct table_item *table,
size_t table_size,
size_t *out_index)
{
uint64_t hash = 0;
fx_status status = fx_value_hash(v, &hash);
if (!FX_OK(status)) {
return status;
}
size_t index = hash % table_size, i = 1;
bool index_found = false;
while (table[index].i_item && i <= table_size) {
const struct fx_hashtable_item_p *item = fx_object_get_private(
table[index].i_item,
FX_TYPE_HASHTABLE_ITEM);
int cmp = fx_value_compare(&item->i_key, v);
if (cmp == 0) {
index_found = true;
break;
}
index = (index + (i * i)) % table_size;
i++;
}
if (!index_found) {
return FX_ERR_NO_ENTRY;
}
assert(index < table_size);
*out_index = index;
return FX_SUCCESS;
}
static fx_status allocate_index_for_key(
const fx_value *v,
struct table_item *table,
size_t table_size,
size_t *out_index)
{
uint64_t hash = 0;
fx_status status = fx_value_hash(v, &hash);
if (!FX_OK(status)) {
return status;
}
size_t index = hash % table_size;
bool ok = false;
for (size_t i = 1; i < table_size; i++) {
if (!table[index].i_item) {
ok = true;
break;
}
const struct fx_hashtable_item_p *item = fx_object_get_private(
table[index].i_item,
FX_TYPE_HASHTABLE_ITEM);
int cmp = fx_value_compare(&item->i_key, v);
if (cmp == 0) {
ok = true;
break;
}
index = (index + (i * i)) % table_size;
i++;
}
if (!ok) {
return FX_ERR_NO_SPACE;
}
assert(index < table_size);
*out_index = index;
return FX_SUCCESS;
}
static fx_status add_item_to_table(
fx_hashtable_item *item,
struct table_item *table,
size_t table_capacity)
{
size_t index = 0;
struct fx_hashtable_item_p *item_p
= fx_object_get_private(item, FX_TYPE_HASHTABLE_ITEM);
fx_status status = allocate_index_for_key(
&item_p->i_key,
table,
table_capacity,
&index);
if (!FX_OK(status)) {
return status;
}
if (index >= table_capacity) {
return FX_ERR_NO_SPACE;
}
struct table_item *bucket = &table[index];
if (bucket->i_item) {
fx_hashtable_item_unref(bucket->i_item);
}
bucket->i_item = item;
return FX_SUCCESS;
}
static fx_status destroy_table(
struct table_item *table,
size_t length,
bool destroy_items)
{
if (!destroy_items) {
free(table);
return FX_SUCCESS;
}
for (size_t i = 0; i < length; i++) {
struct table_item *item = &table[i];
if (!item->i_item) {
continue;
}
fx_hashtable_item_unref(item->i_item);
}
free(table);
return FX_SUCCESS;
}
static fx_status migrate_table(
struct table_item *src_table,
size_t src_length,
struct table_item *dest_table,
size_t dest_capacity)
{
for (size_t i = 0; i < src_length; i++) {
struct table_item *item = &src_table[i];
if (!item->i_item) {
continue;
}
add_item_to_table(item->i_item, dest_table, dest_capacity);
}
return FX_SUCCESS;
}
static fx_status resize_table(
struct fx_hashtable_p *hashtable,
size_t new_capacity_index)
{
size_t capacity = primes[new_capacity_index];
struct table_item *new_table = calloc(capacity, sizeof *new_table);
if (!new_table) {
return FX_ERR_NO_MEMORY;
}
fx_status status = migrate_table(
hashtable->t_items,
primes[hashtable->t_max_index],
new_table,
capacity);
if (!FX_OK(status)) {
free(new_table);
return status;
}
free(hashtable->t_items);
hashtable->t_items = new_table;
hashtable->t_max_index = new_capacity_index;
return FX_SUCCESS;
}
static fx_status hashtable_put(
struct fx_hashtable_p *hashtable,
fx_value *key,
fx_value *value)
{
fx_status status = FX_SUCCESS;
size_t capacity = primes[hashtable->t_max_index];
if (hashtable->t_count + 1 > capacity) {
status = resize_table(hashtable, hashtable->t_max_index + 1);
if (!FX_OK(status)) {
return status;
}
capacity = primes[hashtable->t_max_index];
}
size_t index = 0;
do {
status = allocate_index_for_key(
key,
hashtable->t_items,
capacity,
&index);
if (status == FX_ERR_NO_SPACE) {
status = resize_table(
hashtable,
hashtable->t_max_index + 1);
if (!FX_OK(status)) {
return status;
}
capacity = primes[hashtable->t_max_index];
continue;
}
if (!FX_OK(status)) {
return status;
}
break;
} while (1);
fx_hashtable_item *item = fx_object_create(FX_TYPE_HASHTABLE_ITEM);
if (!item) {
return FX_ERR_NO_MEMORY;
}
struct fx_hashtable_item_p *item_p
= fx_object_get_private(item, FX_TYPE_HASHTABLE_ITEM);
fx_value_copy(&item_p->i_key, key);
fx_value_copy(&item_p->i_value, value);
hashtable->t_items[index].i_item = item;
hashtable->t_count++;
return FX_SUCCESS;
}
static const fx_value *hashtable_get(
const struct fx_hashtable_p *hashtable,
const fx_value *key)
{
size_t index = 0;
size_t capacity = primes[hashtable->t_max_index];
fx_status status = convert_key_to_index(
key,
hashtable->t_items,
capacity,
&index);
if (!FX_OK(status)) {
return NULL;
}
const fx_hashtable_item *item = hashtable->t_items[index].i_item;
if (!item) {
return NULL;
}
const struct fx_hashtable_item_p *item_p
= fx_object_get_private(item, FX_TYPE_HASHTABLE_ITEM);
return &item_p->i_value;
}
static size_t hashtable_get_count(const struct fx_hashtable_p *hashtable)
{
return hashtable->t_count;
}
static bool hashtable_is_empty(const struct fx_hashtable_p *hashtable)
{
return hashtable->t_count == 0;
}
static const fx_value *hashtable_item_get_key(
const struct fx_hashtable_item_p *item)
{
return &item->i_key;
}
static const fx_value *hashtable_item_get_value(
const struct fx_hashtable_item_p *item)
{
return &item->i_value;
}
/*** PUBLIC FUNCTIONS *********************************************************/
fx_status fx_hashtable_put(
fx_hashtable *hashtable,
fx_value *key,
fx_value *value)
{
FX_CLASS_DISPATCH_STATIC(
FX_TYPE_HASHTABLE,
hashtable_put,
hashtable,
key,
value);
}
const fx_value *fx_hashtable_get(
const fx_hashtable *hashtable,
const fx_value *key)
{
FX_CLASS_DISPATCH_STATIC(
FX_TYPE_HASHTABLE,
hashtable_get,
hashtable,
key);
}
size_t fx_hashtable_get_count(const fx_hashtable *hashtable)
{
FX_CLASS_DISPATCH_STATIC_0(
FX_TYPE_HASHTABLE,
hashtable_get_count,
hashtable);
}
bool fx_hashtable_is_empty(const fx_hashtable *hashtable)
{
FX_CLASS_DISPATCH_STATIC_0(
FX_TYPE_HASHTABLE,
hashtable_is_empty,
hashtable);
}
const fx_value *fx_hashtable_item_get_key(const fx_hashtable_item *item)
{
FX_CLASS_DISPATCH_STATIC_0(
FX_TYPE_HASHTABLE_ITEM,
hashtable_item_get_key,
item);
}
const fx_value *fx_hashtable_item_get_value(const fx_hashtable_item *item)
{
FX_CLASS_DISPATCH_STATIC_0(
FX_TYPE_HASHTABLE_ITEM,
hashtable_item_get_value,
item);
}
const fx_iterator *fx_hashtable_begin(const fx_hashtable *hashtable)
{
fx_hashtable_iterator *it_obj
= fx_object_create(FX_TYPE_HASHTABLE_ITERATOR);
struct fx_hashtable_iterator_p *it
= fx_object_get_private(it_obj, FX_TYPE_HASHTABLE_ITERATOR);
it->_h = hashtable;
it->_h_p = fx_object_get_private(hashtable, FX_TYPE_HASHTABLE);
it->i = 0;
if (fx_hashtable_is_empty(hashtable)) {
fx_iterator_set_status(it_obj, FX_ERR_NO_DATA);
return it_obj;
}
struct table_item *table = it->_h_p->t_items;
size_t capacity = primes[it->_h_p->t_max_index];
while (it->i < capacity && table[it->i].i_item == NULL) {
it->i++;
}
it->item_value = FX_VALUE_OBJECT_REF(table[it->i].i_item);
return it_obj;
}
/*** VIRTUAL FUNCTIONS ********************************************************/
static void hashtable_init(fx_object *obj, void *priv)
{
struct fx_hashtable_p *map = priv;
map->t_max_index = 0;
map->t_count = 0;
size_t capacity = primes[map->t_max_index];
map->t_items = calloc(capacity, sizeof *map->t_items);
}
static void hashtable_fini(fx_object *obj, void *priv)
{
struct fx_hashtable_p *map = priv;
size_t capacity = primes[map->t_max_index];
size_t nr_destroyed = 0;
for (size_t i = 0; i < capacity; i++) {
if (map->t_items[i].i_item) {
nr_destroyed++;
fx_hashtable_item_unref(map->t_items[i].i_item);
}
}
free(map->t_items);
}
static void hashtable_item_fini(fx_object *obj, void *priv)
{
struct fx_hashtable_item_p *item = priv;
fx_value_unset(&item->i_key);
fx_value_unset(&item->i_value);
}
static void hashtable_iterator_fini(fx_object *obj, void *priv)
{
struct fx_hashtable_iterator_p *it = priv;
fx_value_unset(&it->item_value);
}
static fx_status to_string(
const fx_value *obj,
fx_stream *out,
const char *format)
{
struct fx_hashtable_p *hashtable
= fx_object_get_private(obj->v_object, FX_TYPE_HASHTABLE);
if (!hashtable->t_count) {
fx_stream_write_cstr(out, "{}", NULL);
return FX_SUCCESS;
}
fx_stream_write_cstr(out, "{\n", NULL);
fx_stream_push_indent(out, 1);
size_t nr_written = 0;
size_t capacity = primes[hashtable->t_max_index];
for (size_t i = 0; i < capacity; i++) {
fx_hashtable_item *item = hashtable->t_items[i].i_item;
if (!item) {
continue;
}
nr_written++;
struct fx_hashtable_item_p *item_p
= fx_object_get_private(item, FX_TYPE_HASHTABLE_ITEM);
const char *cstr = NULL;
fx_value_get_cstr(&item_p->i_key, &cstr);
if (cstr) {
fx_stream_write_char(out, '"');
}
fx_value_to_string(&item_p->i_key, out, NULL);
if (cstr) {
fx_stream_write_char(out, '"');
}
fx_stream_write_cstr(out, " = ", NULL);
fx_value_get_cstr(&item_p->i_value, &cstr);
if (cstr) {
fx_stream_write_char(out, '"');
}
fx_value_to_string(&item_p->i_value, out, NULL);
if (cstr) {
fx_stream_write_char(out, '"');
}
if (nr_written != hashtable->t_count) {
fx_stream_write_char(out, ',');
}
fx_stream_write_char(out, '\n');
}
fx_stream_pop_indent(out);
fx_stream_write_char(out, '}');
return FX_SUCCESS;
}
static fx_status get_key(
const fx_value *item_value,
const fx_property *prop,
fx_value *out)
{
fx_hashtable_item *item = NULL;
fx_value_get_object(item_value, &item);
if (!item) {
return FX_ERR_NOT_SUPPORTED;
}
struct fx_hashtable_item_p *item_p
= fx_object_get_private(item, FX_TYPE_HASHTABLE_ITEM);
fx_value_copy(out, &item_p->i_key);
return FX_SUCCESS;
}
static fx_status get_value(
const fx_value *item_value,
const fx_property *prop,
fx_value *out)
{
fx_hashtable_item *item = NULL;
fx_value_get_object(item_value, &item);
if (!item) {
return FX_ERR_NOT_SUPPORTED;
}
struct fx_hashtable_item_p *item_p
= fx_object_get_private(item, FX_TYPE_HASHTABLE_ITEM);
fx_value_copy(out, &item_p->i_value);
return FX_SUCCESS;
}
/*** ITERATOR FUNCTIONS *******************************************************/
static enum fx_status iterator_move_next(const fx_iterator *obj)
{
struct fx_hashtable_iterator_p *it
= fx_object_get_private(obj, FX_TYPE_HASHTABLE_ITERATOR);
fx_value_unset(&it->item_value);
struct table_item *table = it->_h_p->t_items;
size_t capacity = primes[it->_h_p->t_max_index];
it->i++;
while (it->i < capacity && table[it->i].i_item == NULL) {
it->i++;
}
if (it->i >= capacity || !table[it->i].i_item) {
return FX_ERR_NO_DATA;
}
it->item_value = FX_VALUE_OBJECT_REF(table[it->i].i_item);
return FX_SUCCESS;
}
static enum fx_status iterator_erase(fx_iterator *obj)
{
struct fx_hashtable_iterator_p *it
= fx_object_get_private(obj, FX_TYPE_HASHTABLE_ITERATOR);
struct table_item *table = it->_h_p->t_items;
size_t capacity = primes[it->_h_p->t_max_index];
if (table[it->i].i_item) {
fx_hashtable_item_unref(table[it->i].i_item);
table[it->i].i_item = NULL;
}
it->i++;
while (it->i < capacity && table[it->i].i_item == NULL) {
it->i++;
}
if (it->i >= capacity || !table[it->i].i_item) {
return FX_ERR_NO_DATA;
}
return FX_SUCCESS;
}
static const fx_value *iterator_get_value(const fx_iterator *obj)
{
struct fx_hashtable_iterator_p *it
= fx_object_get_private(obj, FX_TYPE_HASHTABLE_ITERATOR);
struct table_item *table = it->_h_p->t_items;
size_t capacity = primes[it->_h_p->t_max_index];
if (table[it->i].i_item) {
return &it->item_value;
}
return NULL;
}
/*** CLASS DEFINITION *********************************************************/
// ---- fx_hashtable DEFINITION
FX_TYPE_CLASS_BEGIN(fx_hashtable)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = to_string;
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterable, FX_TYPE_ITERABLE)
FX_INTERFACE_ENTRY(it_begin) = fx_hashtable_begin;
FX_TYPE_VTABLE_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE)
FX_TYPE_CLASS_END(fx_hashtable)
FX_TYPE_DEFINITION_BEGIN(fx_hashtable)
FX_TYPE_ID(0xc73b431c, 0x5bd5, 0x45ac, 0x888b, 0x35d352db1fe4);
FX_TYPE_NAME("fx.collections.hashtable");
FX_TYPE_CLASS(fx_hashtable_class);
FX_TYPE_IMPLEMENTS(FX_TYPE_ITERABLE);
FX_TYPE_INSTANCE_PRIVATE(struct fx_hashtable_p);
FX_TYPE_INSTANCE_INIT(hashtable_init);
FX_TYPE_INSTANCE_FINI(hashtable_fini);
FX_TYPE_DEFINITION_END(fx_hashtable)
// ---- fx_hashtable_iterator DEFINITION
FX_TYPE_CLASS_BEGIN(fx_hashtable_iterator)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterator, FX_TYPE_ITERATOR)
FX_INTERFACE_ENTRY(it_move_next) = iterator_move_next;
FX_INTERFACE_ENTRY(it_erase) = iterator_erase;
FX_INTERFACE_ENTRY(it_get_value) = iterator_get_value;
FX_TYPE_VTABLE_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR)
FX_TYPE_CLASS_END(fx_hashtable_iterator)
FX_TYPE_DEFINITION_BEGIN(fx_hashtable_iterator)
FX_TYPE_ID(0x42c6836d, 0xf801, 0x49f9, 0xba58, 0xf9cd6f1cb22d);
FX_TYPE_NAME("fx.collections.hashtable.iterator");
FX_TYPE_EXTENDS(FX_TYPE_ITERATOR);
FX_TYPE_CLASS(fx_hashtable_iterator_class);
FX_TYPE_INSTANCE_PRIVATE(struct fx_hashtable_iterator_p);
FX_TYPE_INSTANCE_FINI(hashtable_iterator_fini);
FX_TYPE_DEFINITION_END(fx_hashtable_iterator)
// ---- fx_hashtable_item DEFINITION
FX_TYPE_CLASS_BEGIN(fx_hashtable_item)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_PROPERTY("key", get_key, NULL);
FX_TYPE_PROPERTY("value", get_value, NULL);
FX_TYPE_CLASS_END(fx_hashtable_item)
FX_TYPE_DEFINITION_BEGIN(fx_hashtable_item)
FX_TYPE_ID(0x748f78f3, 0x53df, 0x406c, 0x8777, 0x2075bd11fb97);
FX_TYPE_NAME("fx.collections.hashtable.item");
FX_TYPE_EXTENDS(FX_TYPE_OBJECT);
FX_TYPE_CLASS(fx_hashtable_item_class);
FX_TYPE_INSTANCE_PRIVATE(struct fx_hashtable_item_p);
FX_TYPE_INSTANCE_FINI(hashtable_item_fini);
FX_TYPE_DEFINITION_END(fx_hashtable_item)
+15 -173
View File
@@ -1,13 +1,5 @@
/** #ifndef FX_COLLECTIONS_ARRAY_H_
* A heterogeneous array of objects. fx_array only stores references #define FX_COLLECTIONS_ARRAY_H_
* to the objects that it contains, not the object data itself.
*
* fx_array stores pointers to objects in a single contiguous array,
* but this is an implementation detail that may change in the future.
* Users of fx_array should not rely on this being the case.
*/
#ifndef FX_DS_ARRAY_H_
#define FX_DS_ARRAY_H_
#include <fx/iterator.h> #include <fx/iterator.h>
#include <fx/macros.h> #include <fx/macros.h>
@@ -30,192 +22,42 @@ FX_TYPE_CLASS_DECLARATION_END(fx_array)
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_array_iterator) FX_TYPE_CLASS_DECLARATION_BEGIN(fx_array_iterator)
FX_TYPE_CLASS_DECLARATION_END(fx_array_iterator) FX_TYPE_CLASS_DECLARATION_END(fx_array_iterator)
FX_API fx_type fx_array_get_type(void); FX_API fx_type_id fx_array_get_type(void);
FX_API fx_type fx_array_iterator_get_type(void); FX_API fx_type_id fx_array_iterator_get_type(void);
FX_TYPE_DEFAULT_CONSTRUCTOR(fx_array, FX_TYPE_ARRAY); FX_TYPE_DEFAULT_CONSTRUCTOR(fx_array, FX_TYPE_ARRAY);
/**
* Creates an fx_array initialised with the contents of the provided
* fx_object pointer array. The fx_array will take a reference to each
* object specified in `values`, and will increment the reference count.
* The order of objects in the new fx_array will be the same as the order
* of objects in `values`. Any NULL pointers in the `values` array will
* be ignored, and will not result in gaps in the created fx_array.
* However, `nr_values` should be large enough to cover the final
* non-NULL pointer in `values`, including any NULL pointers in-between.
*
* @param values The list of object pointers which should make up the
* contents of the new fx_array.
* @param nr_values The size of the `values` array.
* @return A pointer to the new fx_array, or NULL if an error occurred.
*/
FX_API fx_array *fx_array_create_with_values( FX_API fx_array *fx_array_create_with_values(
fx_object *const *values, const fx_value *values,
size_t nr_values); size_t nr_values);
/**
* Remove all object references from an fx_array, resetting the size of the
* array to zero. The reference counts of all objects in the array will be
* decremented.
*
* @param array The fx_array to clear.
*/
FX_API void fx_array_clear(fx_array *array); FX_API void fx_array_clear(fx_array *array);
/** FX_API fx_status fx_array_push_back(fx_array *array, fx_value value);
* Inserts an object at the end of an fx_array. The reference count of
* the object will be incremented.
*
* @param array The fx_array to append the object to.
* @param value The object to append.
* @return FX_SUCCESS if the object was appended successfully, or an
* error code if an error occurred.
*/
FX_API fx_status fx_array_append(fx_array *array, fx_object *value);
/** FX_API fx_status fx_array_push_front(fx_array *array, fx_value value);
* Inserts an object at the beginning of an fx_array. The reference count
* of the object will be incremented. All other objects in the array
* will be moved to make space for the object being pre-pended.
*
* @param array The fx_array to prepend the object to.
* @param value The object to prepend.
* @return FX_SUCCESS if the object was prepended successfully, or an
* error code if an error occurred.
*/
FX_API fx_status fx_array_prepend(fx_array *array, fx_object *value);
/** FX_API fx_status fx_array_insert(fx_array *array, fx_value value, size_t at);
* Inserts an object into an fx_array at a given index. The reference
* count of the object will be incremented. If the specified index is at
* the beginning or mid-way through the array (i.e. not at the end),
* some or all of the objects already in the array will be moved to make
* space for the object being inserted.
*
* @param array The fx_array to insert the object into.
* @param value The object to insert.
* @param at The index to insert the object at. If the index is
* `FX_NPOS`, the object will be inserted at the end of the fx_array.
* @return FX_SUCCESS if the object was inserted, or a status code
* describing any error that occurred.
*/
FX_API fx_status fx_array_insert(fx_array *array, fx_object *value, size_t at);
/**
* Removes the object at the specified index from an fx_array. The
* reference count of the removed object will be decremented. If the
* specified index is at the beginning or mid-way through the array
* (i.e. not at the end), the remaining objects will be moved to fill
* the empty space created by the object's removal.
*
* @param array The fx_array to remove the object from.
* @param at The index of the object to be removed.
* @return FX_SUCCESS if the object was removed, or a status code
* describing any error that occurred.
*/
FX_API fx_status fx_array_remove(fx_array *array, size_t at); FX_API fx_status fx_array_remove(fx_array *array, size_t at);
/**
* Removes the object at the beginning of an fx_array. The reference count
* of the removed object will be decremented. The remaining objects will be
* moved to fill the empty space created by the object's removal.
*
* @param array The fx_array to remove the object from.
* @return FX_SUCCESS if the object was removed, or a status code describing any
* error that occurred.
*/
FX_API fx_status fx_array_remove_front(fx_array *array); FX_API fx_status fx_array_remove_front(fx_array *array);
/**
* Removes the object at the end of an fx_array. The reference count
* of the removed object will be decremented.
*
* @param array The fx_array to remove the object from.
* @return FX_SUCCESS if the object was removed, or a status code describing any
* error that occurred.
*/
FX_API fx_status fx_array_remove_back(fx_array *array); FX_API fx_status fx_array_remove_back(fx_array *array);
/** FX_API fx_value fx_array_pop(fx_array *array, size_t at);
* Removes the object at the specified index of an fx_array, and returns
* a pointer to it. The reference count of the removed object will NOT
* be decremented. The caller becomes the owner of the array's reference
* to the object. If the specified index is at the beginning or mid-way
* through the array (i.e. not at the end), the remaining objects will
* be moved to fill the empty space created by the object's removal.
*
* @param array The fx_array to remove the object from.
* @param at The index of the object to be removed.
* @return An pointer to the removed object. This pointer is owned by
* the caller. Returns NULL if an error occurred.
*/
FX_API fx_object *fx_array_pop(fx_array *array, size_t at);
/** FX_API fx_value fx_array_pop_front(fx_array *array);
* Removes the object at the beginning of an fx_array, and returns a
* pointer to it. The reference count of the removed object will NOT be
* decremented. The caller becomes the owner of the array's reference to
* the object. The remaining objects in the fx_array will be moved to
* fill the empty space left by the removed object.
*
* @param array The fx_array to remove the object from.
* @return An pointer to the removed object. This pointer is owned by
* the caller. Returns NULL if an error occurred.
*/
FX_API fx_object *fx_array_pop_front(fx_array *array);
/** FX_API fx_value fx_array_pop_back(fx_array *array);
* Removes the object at the end of an fx_array, and returns a pointer to it.
* The reference count of the removed object will NOT be decremented. The caller
* becomes the owner of the array's reference to the object.
*
* @param array The fx_array to remove the object from.
* @return An pointer to the removed object. This pointer is owned by the
* caller. Returns NULL if an error occurred.
*/
FX_API fx_object *fx_array_pop_back(fx_array *array);
/** FX_API fx_value *fx_array_get_ref(const fx_array *array, size_t at);
* Returns an unowned pointer to the object at the given index of an fx_array.
* The caller does not own the returned pointer, and MUST NOT release it.
*
* @param array The fx_array.
* @param at The index of the object to return.
* @return A pointer to the object at the given index. This pointer is NOT owned
* by the caller. Returns NULL if an error occurred.
*/
FX_API fx_object *fx_array_at(const fx_array *array, size_t at);
/** FX_API fx_value fx_array_get(const fx_array *array, size_t at);
* Returns an owned pointer to the object at the given index of an
* fx_array. The caller owns the returned pointer, and must release it
* when they are finished with it.
*
* @param array The fx_array.
* @param at The index of the object to return.
* @return A pointer to the object at the given index. This pointer is
* owned by the caller. Returns NULL if an error occurred.
*/
FX_API fx_object *fx_array_get(fx_array *array, size_t at);
/** FX_API size_t fx_array_get_size(const fx_array *array);
* Returns the number of objects contained in an fx_array.
*
* @param array The fx_array.
* @return The number of objects contained in the fx_array.
*/
FX_API size_t fx_array_size(const fx_array *array);
/** FX_API size_t fx_array_get_capacity(const fx_array *array);
* Returns the current maximum capacity of an fx_array. This represents
* the number of objects that can be stored in an fx_array before its
* internal buffer would need to be re-sized.
*
* @param array The fx_array.
* @return The maximum capacity of the fx_array.
*/
FX_API size_t fx_array_capacity(const fx_array *array);
FX_DECLS_END; FX_DECLS_END;
@@ -14,7 +14,7 @@ FX_DECLARE_TYPE(fx_bitmap);
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_bitmap) FX_TYPE_CLASS_DECLARATION_BEGIN(fx_bitmap)
FX_TYPE_CLASS_DECLARATION_END(fx_bitmap) FX_TYPE_CLASS_DECLARATION_END(fx_bitmap)
FX_API fx_type fx_bitmap_get_type(void); FX_API fx_type_id fx_bitmap_get_type(void);
FX_API fx_bitmap *fx_bitmap_create(size_t nr_bits); FX_API fx_bitmap *fx_bitmap_create(size_t nr_bits);
@@ -13,7 +13,7 @@ FX_DECLARE_TYPE(fx_buffer);
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_buffer) FX_TYPE_CLASS_DECLARATION_BEGIN(fx_buffer)
FX_TYPE_CLASS_DECLARATION_END(fx_buffer) FX_TYPE_CLASS_DECLARATION_END(fx_buffer)
FX_API fx_type fx_buffer_get_type(void); FX_API fx_type_id fx_buffer_get_type(void);
FX_API fx_buffer *fx_buffer_create(size_t item_sz); 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_bytes(const void *p, size_t len);
@@ -1,64 +0,0 @@
#ifndef FX_DS_DICT_H_
#define FX_DS_DICT_H_
#include <fx/bst.h>
#include <fx/macros.h>
#include <fx/misc.h>
#include <fx/queue.h>
#include <fx/status.h>
#include <fx/string.h>
FX_DECLS_BEGIN;
#define FX_TYPE_DICT (fx_dict_get_type())
#define FX_TYPE_DICT_ITERATOR (fx_dict_iterator_get_type())
struct fx_dict_p;
FX_DECLARE_TYPE(fx_dict);
FX_DECLARE_TYPE(fx_dict_iterator);
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_dict)
FX_TYPE_CLASS_DECLARATION_END(fx_dict)
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_dict_iterator)
FX_TYPE_CLASS_DECLARATION_END(fx_dict_iterator)
#define FX_DICT_ITEM(k, v) {.key = (k), .value = (v)}
#define FX_DICT_ITEM_END {.key = NULL, .value = NULL}
#define fx_dict_foreach(it, dict) \
for (int z__fx_unique_name() = fx_dict_iterator_begin(dict, it); \
(it)->key != NULL; \
fx_dict_iterator_next(it))
typedef struct fx_dict_item {
const fx_string *key;
fx_object *value;
} fx_dict_item;
FX_API fx_type 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
@@ -2,6 +2,7 @@
#define FX_DS_HASHMAP_H_ #define FX_DS_HASHMAP_H_
#include <fx/bst.h> #include <fx/bst.h>
#include <fx/iterator.h>
#include <fx/macros.h> #include <fx/macros.h>
#include <fx/misc.h> #include <fx/misc.h>
#include <fx/queue.h> #include <fx/queue.h>
@@ -60,8 +61,8 @@ typedef struct fx_hashmap_item {
fx_hashmap_value value; fx_hashmap_value value;
} fx_hashmap_item; } fx_hashmap_item;
FX_API fx_type fx_hashmap_get_type(void); FX_API fx_type_id fx_hashmap_get_type(void);
FX_API fx_type fx_hashmap_iterator_get_type(void); FX_API fx_type_id fx_hashmap_iterator_get_type(void);
FX_API fx_hashmap *fx_hashmap_create( FX_API fx_hashmap *fx_hashmap_create(
fx_hashmap_key_destructor key_dtor, fx_hashmap_key_destructor key_dtor,
@@ -82,7 +83,7 @@ FX_API bool fx_hashmap_has_key(
FX_API size_t fx_hashmap_get_size(const fx_hashmap *hashmap); FX_API size_t fx_hashmap_get_size(const fx_hashmap *hashmap);
FX_API bool fx_hashmap_is_empty(const fx_hashmap *hashmap); FX_API bool fx_hashmap_is_empty(const fx_hashmap *hashmap);
FX_API fx_iterator *fx_hashmap_begin(fx_hashmap *hashmap); FX_API const fx_iterator *fx_hashmap_begin(const fx_hashmap *hashmap);
FX_API const fx_iterator *fx_hashmap_cbegin(const fx_hashmap *hashmap); FX_API const fx_iterator *fx_hashmap_cbegin(const fx_hashmap *hashmap);
FX_DECLS_END; FX_DECLS_END;
@@ -0,0 +1,56 @@
#ifndef FX_COLLECTIONS_HASHTABLE_H_
#define FX_COLLECTIONS_HASHTABLE_H_
#include <fx/bst.h>
#include <fx/iterator.h>
#include <fx/macros.h>
#include <fx/misc.h>
#include <fx/queue.h>
#include <fx/status.h>
#include <fx/value.h>
#include <stddef.h>
FX_DECLS_BEGIN;
#define FX_TYPE_HASHTABLE (fx_hashtable_get_type())
#define FX_TYPE_HASHTABLE_ITERATOR (fx_hashtable_iterator_get_type())
#define FX_TYPE_HASHTABLE_ITEM (fx_hashtable_item_get_type())
FX_DECLARE_TYPE(fx_hashtable);
FX_DECLARE_TYPE(fx_hashtable_iterator);
FX_DECLARE_TYPE(fx_hashtable_item);
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_hashtable)
FX_TYPE_CLASS_DECLARATION_END(fx_hashtable)
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_hashtable_iterator)
FX_TYPE_CLASS_DECLARATION_END(fx_hashtable_iterator)
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_hashtable_item)
FX_TYPE_CLASS_DECLARATION_END(fx_hashtable_item)
FX_API fx_type_id fx_hashtable_get_type(void);
FX_API fx_type_id fx_hashtable_iterator_get_type(void);
FX_API fx_type_id fx_hashtable_item_get_type(void);
FX_TYPE_DEFAULT_CONSTRUCTOR(fx_hashtable, FX_TYPE_HASHTABLE);
FX_API fx_hashtable *fx_hashtable_create(void);
FX_API fx_status
fx_hashtable_put(fx_hashtable *hashtable, fx_value *key, fx_value *value);
FX_API const fx_value *fx_hashtable_get(
const fx_hashtable *hashtable,
const fx_value *key);
FX_API size_t fx_hashtable_get_count(const fx_hashtable *hashtable);
FX_API bool fx_hashtable_is_empty(const fx_hashtable *hashtable);
FX_API const fx_iterator *fx_hashtable_begin(const fx_hashtable *hashtable);
FX_API const fx_value *fx_hashtable_item_get_key(const fx_hashtable_item *item);
FX_API const fx_value *fx_hashtable_item_get_value(
const fx_hashtable_item *item);
FX_DECLS_END;
#endif
+3 -4
View File
@@ -23,8 +23,8 @@ FX_TYPE_CLASS_DECLARATION_END(fx_list_iterator)
typedef struct fx_list_entry fx_list_entry; typedef struct fx_list_entry fx_list_entry;
FX_API fx_type fx_list_get_type(void); FX_API fx_type_id fx_list_get_type(void);
FX_API fx_type fx_list_iterator_get_type(void); FX_API fx_type_id fx_list_iterator_get_type(void);
FX_TYPE_DEFAULT_CONSTRUCTOR(fx_list, FX_TYPE_LIST); FX_TYPE_DEFAULT_CONSTRUCTOR(fx_list, FX_TYPE_LIST);
@@ -59,8 +59,7 @@ FX_API void fx_list_delete_all(fx_list *q);
FX_API void *fx_list_entry_value(const fx_list_entry *entry); FX_API void *fx_list_entry_value(const fx_list_entry *entry);
FX_API fx_iterator *fx_list_begin(fx_list *q); FX_API const fx_iterator *fx_list_begin(const fx_list *q);
FX_API const fx_iterator *fx_list_cbegin(const fx_list *q);
FX_DECLS_END; FX_DECLS_END;
@@ -1,256 +0,0 @@
#ifndef FX_DS_NUMBER_H
#define FX_DS_NUMBER_H
#include <fx/macros.h>
#include <stdbool.h>
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
@@ -1,58 +0,0 @@
#ifndef FX_DS_TREE_H_
#define FX_DS_TREE_H_
#include <fx/macros.h>
#include <fx/misc.h>
#include <fx/queue.h>
#include <fx/string.h>
FX_DECLS_BEGIN;
#define FX_TYPE_TREE (fx_tree_get_type())
#define FX_TYPE_TREE_ITERATOR (fx_tree_iterator_get_type())
FX_DECLARE_TYPE(fx_tree);
FX_DECLARE_TYPE(fx_tree_iterator);
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_tree)
FX_TYPE_CLASS_DECLARATION_END(fx_tree)
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_tree_iterator)
FX_TYPE_CLASS_DECLARATION_END(fx_tree_iterator)
#define FX_TREE_NODE_INIT ((fx_tree_node) {0})
#define FX_TREE_CONTAINER(t, m, v) \
((void *)((v) ? (uintptr_t)(v) - (offsetof(t, m)) : 0))
typedef struct fx_tree_node {
struct fx_tree_node *__p01, *__p02, *__p03;
struct fx_queue_entry __q01;
} fx_tree_node;
FX_API fx_type 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
+61 -68
View File
@@ -23,6 +23,7 @@ struct fx_list_iterator_p {
size_t i; size_t i;
void *item; void *item;
fx_value item_value;
fx_list_entry *entry; fx_list_entry *entry;
}; };
@@ -41,8 +42,10 @@ static void *list_first_item(const struct fx_list_p *q)
return NULL; return NULL;
} }
struct fx_list_entry *list_entry struct fx_list_entry *list_entry = fx_unbox(
= fx_unbox(struct fx_list_entry, entry, e_entry); struct fx_list_entry,
entry,
e_entry);
return list_entry->e_data; return list_entry->e_data;
} }
@@ -55,8 +58,10 @@ static void *list_last_item(const struct fx_list_p *q)
return NULL; return NULL;
} }
struct fx_list_entry *list_entry struct fx_list_entry *list_entry = fx_unbox(
= fx_unbox(struct fx_list_entry, entry, e_entry); struct fx_list_entry,
entry,
e_entry);
return list_entry->e_data; return list_entry->e_data;
} }
@@ -69,8 +74,10 @@ static struct fx_list_entry *list_first_entry(const struct fx_list_p *q)
return NULL; return NULL;
} }
struct fx_list_entry *list_entry struct fx_list_entry *list_entry = fx_unbox(
= fx_unbox(struct fx_list_entry, entry, e_entry); struct fx_list_entry,
entry,
e_entry);
return list_entry; return list_entry;
} }
@@ -83,8 +90,10 @@ static struct fx_list_entry *list_last_entry(const struct fx_list_p *q)
return NULL; return NULL;
} }
struct fx_list_entry *list_entry struct fx_list_entry *list_entry = fx_unbox(
= fx_unbox(struct fx_list_entry, entry, e_entry); struct fx_list_entry,
entry,
e_entry);
return list_entry; return list_entry;
} }
@@ -169,8 +178,10 @@ static void *list_pop_front(struct fx_list_p *q)
return NULL; return NULL;
} }
struct fx_list_entry *list_entry struct fx_list_entry *list_entry = fx_unbox(
= fx_unbox(struct fx_list_entry, entry, e_entry); struct fx_list_entry,
entry,
e_entry);
void *item = list_entry->e_data; void *item = list_entry->e_data;
free(list_entry); free(list_entry);
@@ -187,8 +198,10 @@ static void *list_pop_back(struct fx_list_p *q)
return NULL; return NULL;
} }
struct fx_list_entry *list_entry struct fx_list_entry *list_entry = fx_unbox(
= fx_unbox(struct fx_list_entry, entry, e_entry); struct fx_list_entry,
entry,
e_entry);
void *item = list_entry->e_data; void *item = list_entry->e_data;
free(list_entry); free(list_entry);
@@ -202,8 +215,10 @@ static struct fx_list_entry *find_item(struct fx_list_p *list, void *item)
{ {
struct fx_queue_entry *entry = fx_queue_first(&list->l_queue); struct fx_queue_entry *entry = fx_queue_first(&list->l_queue);
while (entry) { while (entry) {
struct fx_list_entry *list_entry struct fx_list_entry *list_entry = fx_unbox(
= fx_unbox(struct fx_list_entry, entry, e_entry); struct fx_list_entry,
entry,
e_entry);
if (list_entry->e_data == item) { if (list_entry->e_data == item) {
return list_entry; return list_entry;
@@ -246,8 +261,10 @@ static void list_delete_all(struct fx_list_p *q)
{ {
struct fx_queue_entry *entry = fx_queue_first(&q->l_queue); struct fx_queue_entry *entry = fx_queue_first(&q->l_queue);
while (entry) { while (entry) {
struct fx_list_entry *list_entry struct fx_list_entry *list_entry = fx_unbox(
= fx_unbox(struct fx_list_entry, entry, e_entry); struct fx_list_entry,
entry,
e_entry);
struct fx_queue_entry *next = fx_queue_next(entry); struct fx_queue_entry *next = fx_queue_next(entry);
free(list_entry); free(list_entry);
@@ -385,30 +402,12 @@ void *fx_list_entry_value(const struct fx_list_entry *entry)
return entry ? entry->e_data : NULL; return entry ? entry->e_data : NULL;
} }
fx_iterator *fx_list_begin(fx_list *q) const fx_iterator *fx_list_begin(const fx_list *q)
{ {
fx_list_iterator *it_obj = fx_object_create(FX_TYPE_LIST_ITERATOR); fx_list_iterator *it_obj = fx_object_create(FX_TYPE_LIST_ITERATOR);
struct fx_list_iterator_p *it struct fx_list_iterator_p *it = fx_object_get_private(
= fx_object_get_private(it_obj, FX_TYPE_LIST_ITERATOR); it_obj,
FX_TYPE_LIST_ITERATOR);
it->_q = q;
it->_q_p = fx_object_get_private(q, FX_TYPE_LIST);
it->_q_entry = fx_queue_first(&it->_q_p->l_queue);
it->i = 0;
it->entry = fx_unbox(struct fx_list_entry, it->_q_entry, e_entry);
if (it->entry) {
it->item = it->entry->e_data;
}
return 0;
}
const fx_iterator *fx_list_cbegin(const fx_list *q)
{
fx_list_iterator *it_obj = fx_object_create(FX_TYPE_LIST_ITERATOR);
struct fx_list_iterator_p *it
= fx_object_get_private(it_obj, FX_TYPE_LIST_ITERATOR);
it->_q = (fx_list *)q; it->_q = (fx_list *)q;
it->_q_p = fx_object_get_private(q, FX_TYPE_LIST); it->_q_p = fx_object_get_private(q, FX_TYPE_LIST);
@@ -440,8 +439,9 @@ static void list_fini(fx_object *obj, void *priv)
static enum fx_status iterator_move_next(const fx_iterator *obj) static enum fx_status iterator_move_next(const fx_iterator *obj)
{ {
struct fx_list_iterator_p *it struct fx_list_iterator_p *it = fx_object_get_private(
= fx_object_get_private(obj, FX_TYPE_LIST_ITERATOR); obj,
FX_TYPE_LIST_ITERATOR);
if (!it->_q_entry) { if (!it->_q_entry) {
it->entry = NULL; it->entry = NULL;
@@ -467,8 +467,9 @@ static enum fx_status iterator_move_next(const fx_iterator *obj)
static enum fx_status iterator_erase(fx_iterator *obj) static enum fx_status iterator_erase(fx_iterator *obj)
{ {
struct fx_list_iterator_p *it struct fx_list_iterator_p *it = fx_object_get_private(
= fx_object_get_private(obj, FX_TYPE_LIST_ITERATOR); obj,
FX_TYPE_LIST_ITERATOR);
if (!it->entry || !it->_q_entry) { if (!it->entry || !it->_q_entry) {
return FX_ERR_OUT_OF_BOUNDS; return FX_ERR_OUT_OF_BOUNDS;
@@ -488,35 +489,28 @@ static enum fx_status iterator_erase(fx_iterator *obj)
return FX_SUCCESS; return FX_SUCCESS;
} }
static fx_iterator_value iterator_get_value(fx_iterator *obj) static const fx_value *iterator_get_value(const fx_iterator *obj)
{ {
struct fx_list_iterator_p *it struct fx_list_iterator_p *it = fx_object_get_private(
= fx_object_get_private(obj, FX_TYPE_LIST_ITERATOR); obj,
FX_TYPE_LIST_ITERATOR);
return FX_ITERATOR_VALUE_PTR(it->item); it->item_value = FX_POINTER(it->item);
} return &it->item_value;
static const fx_iterator_value iterator_get_cvalue(const fx_iterator *obj)
{
struct fx_list_iterator_p *it
= fx_object_get_private(obj, FX_TYPE_LIST_ITERATOR);
return FX_ITERATOR_VALUE_CPTR(it->item);
} }
/*** CLASS DEFINITION *********************************************************/ /*** CLASS DEFINITION *********************************************************/
// ---- fx_list DEFINITION // ---- fx_list DEFINITION
FX_TYPE_CLASS_DEFINITION_BEGIN(fx_list) FX_TYPE_CLASS_BEGIN(fx_list)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL; FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT) FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_iterable, FX_TYPE_ITERABLE) FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterable, FX_TYPE_ITERABLE)
FX_INTERFACE_ENTRY(it_begin) = fx_list_begin; FX_INTERFACE_ENTRY(it_begin) = fx_list_begin;
FX_INTERFACE_ENTRY(it_cbegin) = fx_list_cbegin; FX_TYPE_VTABLE_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE)
FX_TYPE_CLASS_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE) FX_TYPE_CLASS_END(fx_list)
FX_TYPE_CLASS_DEFINITION_END(fx_list)
FX_TYPE_DEFINITION_BEGIN(fx_list) FX_TYPE_DEFINITION_BEGIN(fx_list)
FX_TYPE_ID(0x8730e66f, 0x0fd9, 0x4773, 0x9bbd, 0x6428f6e495eb); FX_TYPE_ID(0x8730e66f, 0x0fd9, 0x4773, 0x9bbd, 0x6428f6e495eb);
@@ -528,18 +522,17 @@ FX_TYPE_DEFINITION_BEGIN(fx_list)
FX_TYPE_DEFINITION_END(fx_list) FX_TYPE_DEFINITION_END(fx_list)
// ---- fx_list_iterator DEFINITION // ---- fx_list_iterator DEFINITION
FX_TYPE_CLASS_DEFINITION_BEGIN(fx_list_iterator) FX_TYPE_CLASS_BEGIN(fx_list_iterator)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL; FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT) FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_iterator, FX_TYPE_ITERATOR) FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterator, FX_TYPE_ITERATOR)
FX_INTERFACE_ENTRY(it_move_next) = iterator_move_next; FX_INTERFACE_ENTRY(it_move_next) = iterator_move_next;
FX_INTERFACE_ENTRY(it_erase) = iterator_erase; FX_INTERFACE_ENTRY(it_erase) = iterator_erase;
FX_INTERFACE_ENTRY(it_get_value) = iterator_get_value; FX_INTERFACE_ENTRY(it_get_value) = iterator_get_value;
FX_INTERFACE_ENTRY(it_get_cvalue) = iterator_get_cvalue; FX_TYPE_VTABLE_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR)
FX_TYPE_CLASS_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR) FX_TYPE_CLASS_END(fx_list_iterator)
FX_TYPE_CLASS_DEFINITION_END(fx_list_iterator)
FX_TYPE_DEFINITION_BEGIN(fx_list_iterator) FX_TYPE_DEFINITION_BEGIN(fx_list_iterator)
FX_TYPE_ID(0xd9658456, 0xdd80, 0x419a, 0xb23a, 0xb513013e6431); FX_TYPE_ID(0xd9658456, 0xdd80, 0x419a, 0xb23a, 0xb513013e6431);
File diff suppressed because it is too large Load Diff
@@ -1,13 +1,14 @@
#include <fx/ds/array.h> #include <fx/collections/array.h>
#include <fx/ds/number.h> #include <fx/int.h>
#include <stdio.h> #include <stdio.h>
int main(void) int main(void)
{ {
#if 0
fx_array *array = fx_array_create(); fx_array *array = fx_array_create();
fx_array_append(array, FX_RV_INT(32)); fx_array_append(array, fx_int_create(32));
fx_array_append(array, FX_RV_INT(64)); fx_array_append(array, fx_int_create(64));
fx_array_append(array, FX_RV_INT(128)); fx_array_append(array, fx_int_create(128));
fx_iterator *it = fx_iterator_begin(array); fx_iterator *it = fx_iterator_begin(array);
fx_foreach_ptr(fx_object, obj, it) fx_foreach_ptr(fx_object, obj, it)
@@ -17,5 +18,6 @@ int main(void)
fx_iterator_unref(it); fx_iterator_unref(it);
fx_array_unref(array); fx_array_unref(array);
#endif
return 0; return 0;
} }
+49
View File
@@ -0,0 +1,49 @@
#include <fx/collections/hashtable.h>
#include <fx/int.h>
#include <fx/reflection/type.h>
#include <fx/stream.h>
#include <stdio.h>
int main(void)
{
fx_hashtable *ht = fx_hashtable_create();
fx_hashtable_put(ht, &FX_INT(32), &FX_INT(64));
fx_hashtable_put(ht, &FX_CSTR("hello"), &FX_INT(128));
const fx_value *v = fx_hashtable_get(ht, &FX_INT(32));
if (v) {
printf("32= ");
fx_value_to_string(v, fx_stdout, NULL);
printf("\n");
} else {
printf("no value\n");
}
const fx_iterator *it = fx_hashtable_begin(ht);
fx_foreach(val, it)
{
fx_hashtable_item *item = NULL;
fx_value_get_object(val, &item);
printf("item %p\n", item);
const fx_value *key, *value;
key = fx_hashtable_item_get_key(item);
value = fx_hashtable_item_get_value(item);
fx_iterator *prop_it = fx_type_get_properties(
fx_type_get_by_id(FX_TYPE_HASHTABLE_ITEM));
fx_foreach(prop_val, prop_it)
{
fx_property *prop;
fx_value_get_object(prop_val, &prop);
printf("%s = ", fx_property_get_name(prop));
fx_value value = FX_VALUE_EMPTY;
fx_property_get_value(prop, val, &value);
fx_value_to_string(&value, fx_stdout, NULL);
printf("\n");
}
fx_iterator_unref(prop_it);
}
fx_iterator_unref(it);
return 0;
}
+7
View File
@@ -0,0 +1,7 @@
#include <fx/float.h>
#include <stdio.h>
int main(void)
{
return 0;
}
+11
View File
@@ -0,0 +1,11 @@
#include <fx/string.h>
int main(void)
{
fx_string *string = fx_string_create_from_cstr("Hello, world!");
printf("string object = ");
fx_object_to_string(string, fx_stdout, NULL);
printf("\n");
fx_string_unref(string);
return 0;
}
@@ -1,6 +1,6 @@
#include <fx/core/stream.h> #include <fx/stream.h>
#include <fx/core/stringstream.h> #include <fx/string.h>
#include <fx/ds/string.h> #include <fx/stringstream.h>
#include <stdio.h> #include <stdio.h>
int main(int argc, const char **argv) int main(int argc, const char **argv)
-390
View File
@@ -1,390 +0,0 @@
#include <fx/collections/tree.h>
#include <stdlib.h>
#include <string.h>
#define ITERATOR_RECURSIVE 0x01u
#define ITERATOR_IS_RECURSIVE(it) (((it)->_f01 & ITERATOR_RECURSIVE) != 0)
#define ITERATOR_UNSET_RECURSIVE(it) ((it)->_f01 &= ~ITERATOR_RECURSIVE)
#define ITERATOR_SET_RECURSIVE(it) ((it)->_f01 |= ITERATOR_RECURSIVE)
#define NODE_PARENT(n) ((n)->__p01)
#define NODE_FIRST_CHILD(n) ((n)->__p02)
#define NODE_NEXT_SIBLING(n) ((n)->__p03)
/*** PRIVATE DATA *************************************************************/
struct fx_tree_p {
struct fx_tree_node *t_root;
};
struct fx_tree_iterator_p {
size_t i, depth;
fx_tree_node *node;
unsigned char _f01;
};
/*** PRIVATE FUNCTIONS ********************************************************/
static void tree_set_root(struct fx_tree_p *tree, struct fx_tree_node *node)
{
tree->t_root = node;
}
static const struct fx_tree_node *next_node(
const struct fx_tree_node *node,
bool recursive,
int *depth_diff)
{
if (!node) {
return NULL;
}
if (!recursive) {
node = NODE_NEXT_SIBLING(node);
return node;
}
int d = 0;
struct fx_tree_node *next = NODE_FIRST_CHILD(node);
if (next) {
d = 1;
*depth_diff = d;
return next;
}
const struct fx_tree_node *n = node;
next = NODE_NEXT_SIBLING(n);
while (!next) {
n = NODE_PARENT(n);
if (!n) {
break;
}
d--;
next = NODE_NEXT_SIBLING(n);
}
*depth_diff = d;
return next;
}
static void remove_node(struct fx_tree_node *node)
{
struct fx_tree_node *parent = NODE_PARENT(node);
if (!parent) {
return;
}
struct fx_tree_node *n0 = NULL, *n1 = NULL;
n0 = NODE_FIRST_CHILD(parent);
while (n0) {
if (n0 == node) {
break;
}
n1 = n0;
n0 = NODE_NEXT_SIBLING(n0);
}
if (!n0) {
return;
}
if (n1) {
NODE_NEXT_SIBLING(n1) = NODE_NEXT_SIBLING(n0);
} else {
NODE_FIRST_CHILD(parent) = NODE_NEXT_SIBLING(n0);
}
NODE_PARENT(n0) = NODE_NEXT_SIBLING(n0) = NULL;
}
static void reparent_children(
struct fx_tree_node *old_parent,
struct fx_tree_node *new_parent)
{
struct fx_tree_node *last = NODE_FIRST_CHILD(new_parent);
while (last && NODE_NEXT_SIBLING(last)) {
last = NODE_NEXT_SIBLING(last);
}
struct fx_tree_node *cur = NODE_FIRST_CHILD(old_parent);
while (cur) {
struct fx_tree_node *next = NODE_NEXT_SIBLING(cur);
NODE_PARENT(cur) = new_parent;
NODE_NEXT_SIBLING(cur) = NULL;
if (last) {
NODE_NEXT_SIBLING(last) = cur;
} else {
NODE_FIRST_CHILD(new_parent) = cur;
}
last = cur;
cur = next;
}
}
/*** PUBLIC FUNCTIONS *********************************************************/
void fx_tree_set_root(fx_tree *tree, struct fx_tree_node *node)
{
FX_CLASS_DISPATCH_STATIC(FX_TYPE_TREE, tree_set_root, tree, node);
}
void fx_tree_node_add_child(
struct fx_tree_node *parent,
struct fx_tree_node *child)
{
if (NODE_PARENT(child)) {
return;
}
NODE_PARENT(child) = parent;
if (!NODE_FIRST_CHILD(parent)) {
NODE_FIRST_CHILD(parent) = child;
return;
}
struct fx_tree_node *cur = NODE_FIRST_CHILD(parent);
while (NODE_NEXT_SIBLING(cur)) {
cur = NODE_NEXT_SIBLING(cur);
}
NODE_NEXT_SIBLING(cur) = child;
}
void fx_tree_node_add_sibling(
struct fx_tree_node *node,
struct fx_tree_node *to_add)
{
if (NODE_PARENT(to_add) || !NODE_PARENT(node)) {
return;
}
fx_tree_node_add_child(NODE_PARENT(node), to_add);
}
struct fx_tree_node *fx_tree_node_get_child(
struct fx_tree_node *node,
size_t at)
{
size_t i = 0;
struct fx_tree_node *cur = NODE_FIRST_CHILD(node);
while (i < at) {
if (!cur) {
return NULL;
}
cur = NODE_NEXT_SIBLING(cur);
i++;
}
return cur;
}
fx_iterator *fx_tree_begin(fx_tree *tree)
{
struct fx_tree_p *p = fx_object_get_private(tree, FX_TYPE_TREE);
return fx_tree_node_begin(p->t_root);
}
const fx_iterator *fx_tree_cbegin(const fx_tree *tree)
{
struct fx_tree_p *p = fx_object_get_private(tree, FX_TYPE_TREE);
return fx_tree_node_begin(p->t_root);
}
fx_iterator *fx_tree_node_begin(struct fx_tree_node *node)
{
fx_tree_iterator *it_obj = fx_object_create(FX_TYPE_TREE_ITERATOR);
struct fx_tree_iterator_p *it
= fx_object_get_private(it_obj, FX_TYPE_TREE_ITERATOR);
it->node = NODE_FIRST_CHILD(node);
it->i = 0;
it->depth = 0;
ITERATOR_UNSET_RECURSIVE(it);
if (!it->node) {
fx_iterator_set_status(it_obj, FX_ERR_NO_DATA);
}
return it_obj;
}
const fx_iterator *fx_tree_node_cbegin(const struct fx_tree_node *node)
{
return fx_tree_node_begin((struct fx_tree_node *)node);
}
fx_iterator *fx_tree_node_begin_recursive(struct fx_tree_node *node)
{
fx_tree_iterator *it_obj = fx_object_create(FX_TYPE_TREE_ITERATOR);
struct fx_tree_iterator_p *it
= fx_object_get_private(it_obj, FX_TYPE_TREE_ITERATOR);
it->node = node;
it->i = 0;
it->depth = 0;
ITERATOR_SET_RECURSIVE(it);
if (!it->node) {
fx_iterator_set_status(it_obj, FX_ERR_NO_DATA);
}
return it_obj;
}
const fx_iterator *fx_tree_node_cbegin_recursive(
const struct fx_tree_node *node)
{
return fx_tree_node_begin_recursive((struct fx_tree_node *)node);
}
/*** VIRTUAL FUNCTIONS ********************************************************/
static void tree_init(fx_object *obj, void *priv)
{
struct fx_tree_p *tree = priv;
}
static void tree_fini(fx_object *obj, void *priv)
{
struct fx_tree_p *tree = priv;
}
/*** ITERATOR FUNCTIONS *******************************************************/
static enum fx_status iterator_move_next(const fx_iterator *obj)
{
struct fx_tree_iterator_p *it
= fx_object_get_private(obj, FX_TYPE_TREE_ITERATOR);
int depth_diff = 0;
const struct fx_tree_node *next
= next_node(it->node, ITERATOR_IS_RECURSIVE(it), &depth_diff);
if (next) {
it->depth += depth_diff;
it->i++;
} else {
it->depth = 0;
it->i = 0;
}
it->node = (struct fx_tree_node *)next;
return (it->node != NULL) ? FX_SUCCESS : FX_ERR_NO_DATA;
}
static enum fx_status iterator_erase(fx_iterator *obj)
{
struct fx_tree_iterator_p *it
= fx_object_get_private(obj, FX_TYPE_TREE_ITERATOR);
if (!it->node) {
return FX_ERR_OUT_OF_BOUNDS;
}
struct fx_tree_node *parent = NODE_PARENT(it->node);
if (!parent) {
return FX_ERR_NOT_SUPPORTED;
}
int d = 0;
struct fx_tree_node *n = it->node;
struct fx_tree_node *next = NODE_NEXT_SIBLING(n);
if (!next) {
next = NODE_FIRST_CHILD(n);
}
while (!next) {
n = NODE_PARENT(n);
if (!n) {
break;
}
d--;
next = NODE_NEXT_SIBLING(n);
}
remove_node(it->node);
reparent_children(it->node, parent);
return FX_SUCCESS;
}
static fx_iterator_value iterator_get_value(fx_iterator *obj)
{
struct fx_tree_iterator_p *it
= fx_object_get_private(obj, FX_TYPE_TREE_ITERATOR);
if (!it->node) {
return FX_ITERATOR_VALUE_NULL;
}
return FX_ITERATOR_VALUE_PTR(it->node);
}
static const fx_iterator_value iterator_get_cvalue(const fx_iterator *obj)
{
struct fx_tree_iterator_p *it
= fx_object_get_private(obj, FX_TYPE_TREE_ITERATOR);
if (!it->node) {
return FX_ITERATOR_VALUE_NULL;
}
return FX_ITERATOR_VALUE_CPTR(it->node);
}
/*** CLASS DEFINITION *********************************************************/
// ---- fx_tree DEFINITION
FX_TYPE_CLASS_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)
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)
FX_TYPE_DEFINITION_BEGIN(fx_tree)
FX_TYPE_ID(0x8d8fa36b, 0xc515, 0x4803, 0x8124, 0xfd704f01b8ae);
FX_TYPE_CLASS(fx_tree_class);
FX_TYPE_IMPLEMENTS(FX_TYPE_ITERABLE);
FX_TYPE_INSTANCE_PRIVATE(struct fx_tree_p);
FX_TYPE_INSTANCE_INIT(tree_init);
FX_TYPE_INSTANCE_FINI(tree_fini);
FX_TYPE_DEFINITION_END(fx_tree)
// ---- fx_tree_iterator DEFINITION
FX_TYPE_CLASS_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)
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)
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)
+13 -12
View File
@@ -3,17 +3,18 @@ include(../cmake/Templates.cmake)
find_package(ZSTD) find_package(ZSTD)
if (ZSTD_FOUND) if (ZSTD_FOUND)
set(libs ${libs} ${ZSTD_LIBRARY}) set(internal_libs ${libs} ${ZSTD_LIBRARY})
set(include_dirs ${include_dirs} ${ZSTD_INCLUDE_DIR}) set(internal_include_dirs ${include_dirs} ${ZSTD_INCLUDE_DIR})
set(function_sources ${function_sources} ${CMAKE_CURRENT_SOURCE_DIR}/function/zstd.c) set(internal_defines ${defines} FX_COMPRESSOR_SUPPORTED_ZSTD)
set(defines ${defines} FX_COMPRESSOR_SUPPORTED_ZSTD) set(namespace_sources ${namespace_sources} ${CMAKE_CURRENT_SOURCE_DIR}/function/zstd.c)
message(STATUS "Enabling ZSTD support in fx-compress") message(STATUS "Enabling ZSTD support in fx.compression")
endif () endif ()
add_fx_module( #add_fx_module(
NAME compress # NAME compress
DEPENDENCIES core # DEPENDENCIES core
EXTRA_SOURCES ${function_sources} # EXTRA_SOURCES ${function_sources}
DEFINES ${defines} # DEFINES ${defines}
LIBS ${libs} # LIBS ${libs}
INCLUDE_DIRS ${include_dirs}) # INCLUDE_DIRS ${include_dirs})
export_fx_namespace_details(fx.compression)
+43 -19
View File
@@ -1,6 +1,6 @@
#include <assert.h> #include <assert.h>
#include <fx/compress/compressor.h> #include <fx/compression/compressor.h>
#include <fx/core/ringbuffer.h> #include <fx/ringbuffer.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@@ -36,16 +36,21 @@ struct compressor_data {
/*** PRIVATE FUNCTIONS ********************************************************/ /*** PRIVATE FUNCTIONS ********************************************************/
static enum fx_status compressor_get_data( static enum fx_status compressor_get_data(
fx_compressor *compressor, struct compressor_data *out) fx_compressor *compressor,
struct compressor_data *out)
{ {
out->c_obj = compressor; out->c_obj = compressor;
return fx_object_get_data( return fx_object_get_data(
compressor, FX_TYPE_COMPRESSOR, NULL, (void **)&out->c_data, compressor,
FX_TYPE_COMPRESSOR,
NULL,
(void **)&out->c_data,
(void **)&out->c_ops); (void **)&out->c_ops);
} }
static enum fx_status compressor_get_mode( static enum fx_status compressor_get_mode(
struct compressor_data *p, enum fx_compressor_mode *out) struct compressor_data *p,
enum fx_compressor_mode *out)
{ {
if (out) { if (out) {
*out = p->c_data->c_mode; *out = p->c_data->c_mode;
@@ -55,7 +60,8 @@ static enum fx_status compressor_get_mode(
} }
static enum fx_status compressor_set_mode( static enum fx_status compressor_set_mode(
struct compressor_data *p, enum fx_compressor_mode mode) struct compressor_data *p,
enum fx_compressor_mode mode)
{ {
if (!p->c_ops->c_set_mode) { if (!p->c_ops->c_set_mode) {
return FX_ERR_NOT_SUPPORTED; return FX_ERR_NOT_SUPPORTED;
@@ -65,7 +71,9 @@ static enum fx_status compressor_set_mode(
} }
static enum fx_status compressor_set_buffer( static enum fx_status compressor_set_buffer(
struct compressor_data *p, fx_ringbuffer *inbuf, fx_ringbuffer *outbuf) struct compressor_data *p,
fx_ringbuffer *inbuf,
fx_ringbuffer *outbuf)
{ {
p->c_data->c_in = inbuf; p->c_data->c_in = inbuf;
p->c_data->c_out = outbuf; p->c_data->c_out = outbuf;
@@ -154,14 +162,18 @@ static bool compressor_eof(const struct compressor_data *p)
/*** PUBLIC FUNCTIONS *********************************************************/ /*** PUBLIC FUNCTIONS *********************************************************/
enum fx_status fx_compressor_get_buffer_size( enum fx_status fx_compressor_get_buffer_size(
fx_type type, fx_compressor_mode mode, size_t *inbuf_size, size_t *outbuf_size) fx_type_id type,
fx_compressor_mode mode,
size_t *inbuf_size,
size_t *outbuf_size)
{ {
fx_class *c = fx_class_get(type); fx_class *c = fx_class_get(type);
if (!c) { if (!c) {
return FX_ERR_INVALID_ARGUMENT; return FX_ERR_INVALID_ARGUMENT;
} }
fx_compressor_class *ops = fx_class_get_interface(c, FX_TYPE_COMPRESSOR); fx_compressor_class *ops
= fx_class_get_interface(c, FX_TYPE_COMPRESSOR);
if (!ops) { if (!ops) {
return FX_ERR_INVALID_ARGUMENT; return FX_ERR_INVALID_ARGUMENT;
} }
@@ -174,22 +186,32 @@ enum fx_status fx_compressor_get_buffer_size(
} }
enum fx_status fx_compressor_get_mode( enum fx_status fx_compressor_get_mode(
const fx_compressor *compressor, enum fx_compressor_mode *out) const fx_compressor *compressor,
enum fx_compressor_mode *out)
{ {
COMPRESSOR_DISPATCH_STATIC( COMPRESSOR_DISPATCH_STATIC(
compressor_get_mode, (fx_compressor *)compressor, out); compressor_get_mode,
(fx_compressor *)compressor,
out);
} }
enum fx_status fx_compressor_set_mode( enum fx_status fx_compressor_set_mode(
fx_compressor *compressor, enum fx_compressor_mode mode) fx_compressor *compressor,
enum fx_compressor_mode mode)
{ {
COMPRESSOR_DISPATCH_STATIC(compressor_set_mode, compressor, mode); COMPRESSOR_DISPATCH_STATIC(compressor_set_mode, compressor, mode);
} }
enum fx_status fx_compressor_set_buffer( enum fx_status fx_compressor_set_buffer(
fx_compressor *compressor, fx_ringbuffer *inbuf, fx_ringbuffer *outbuf) fx_compressor *compressor,
fx_ringbuffer *inbuf,
fx_ringbuffer *outbuf)
{ {
COMPRESSOR_DISPATCH_STATIC(compressor_set_buffer, compressor, inbuf, outbuf); COMPRESSOR_DISPATCH_STATIC(
compressor_set_buffer,
compressor,
inbuf,
outbuf);
} }
enum fx_status fx_compressor_step(fx_compressor *compressor) enum fx_status fx_compressor_step(fx_compressor *compressor)
@@ -209,7 +231,9 @@ enum fx_status fx_compressor_reset(fx_compressor *compressor)
bool fx_compressor_eof(const fx_compressor *compressor) bool fx_compressor_eof(const fx_compressor *compressor)
{ {
COMPRESSOR_DISPATCH_STATIC_0(compressor_eof, (fx_compressor *)compressor); COMPRESSOR_DISPATCH_STATIC_0(
compressor_eof,
(fx_compressor *)compressor);
} }
/*** VIRTUAL FUNCTIONS ********************************************************/ /*** VIRTUAL FUNCTIONS ********************************************************/
@@ -224,11 +248,11 @@ static void compressor_fini(fx_object *obj, void *priv)
/*** CLASS DEFINITION *********************************************************/ /*** CLASS DEFINITION *********************************************************/
FX_TYPE_CLASS_DEFINITION_BEGIN(fx_compressor) FX_TYPE_CLASS_BEGIN(fx_compressor)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL; FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT) FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_DEFINITION_END(fx_compressor) FX_TYPE_CLASS_END(fx_compressor)
FX_TYPE_DEFINITION_BEGIN(fx_compressor) FX_TYPE_DEFINITION_BEGIN(fx_compressor)
FX_TYPE_ID(0x452ee0f9, 0xfe12, 0x48a1, 0xb596, 0xad5b7a3940e7); FX_TYPE_ID(0x452ee0f9, 0xfe12, 0x48a1, 0xb596, 0xad5b7a3940e7);
+188 -63
View File
@@ -1,5 +1,5 @@
#include <fx/compress/compressor.h> #include <fx/compression/compressor.h>
#include <fx/compress/cstream.h> #include <fx/compression/cstream.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@@ -17,8 +17,8 @@ struct fx_cstream_p {
* *
* the input buffer holds data that will be provided to the * the input buffer holds data that will be provided to the
* (de)compression function. in compression mode, this data is provided * (de)compression function. in compression mode, this data is provided
* by the code using the cstream (via fx_cstream_write). in decompression * by the code using the cstream (via fx_cstream_write). in
* mode, this data is read from s_endpoint. * decompression mode, this data is read from s_endpoint.
* *
* the output buffer holds data produced by the (de)compression * the output buffer holds data produced by the (de)compression
* function. in compression mode, this data will be written to * function. in compression mode, this data will be written to
@@ -27,7 +27,8 @@ struct fx_cstream_p {
* *
* heavy usage of cstream's compressed sections facility can result * heavy usage of cstream's compressed sections facility can result
* in the input buffer holding uncompressed data while the stream is in * in the input buffer holding uncompressed data while the stream is in
* decompression mode. this is handled by the uncompressed read code path. * decompression mode. this is handled by the uncompressed read code
* path.
*/ */
fx_ringbuffer *s_in, *s_out; fx_ringbuffer *s_in, *s_out;
fx_compressor_mode s_mode; fx_compressor_mode s_mode;
@@ -78,13 +79,23 @@ struct fx_cstream_p {
/*** PRIVATE FUNCTIONS ********************************************************/ /*** PRIVATE FUNCTIONS ********************************************************/
static enum fx_status read_cursor( static enum fx_status read_cursor(
struct fx_cstream_p *stream, void *buf, size_t count, size_t *out_nr_read) struct fx_cstream_p *stream,
void *buf,
size_t count,
size_t *out_nr_read)
{ {
return fx_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 fx_status read_uncompressed( static enum fx_status read_uncompressed(
struct fx_cstream_p *stream, void *buf, size_t count, size_t *out_nr_read) struct fx_cstream_p *stream,
void *buf,
size_t count,
size_t *out_nr_read)
{ {
size_t remaining = count; size_t remaining = count;
unsigned char *dest = buf; unsigned char *dest = buf;
@@ -101,7 +112,9 @@ static enum fx_status read_uncompressed(
const void *data; const void *data;
size_t available; size_t available;
status = fx_ringbuffer_open_read_buffer( status = fx_ringbuffer_open_read_buffer(
stream->s_in, &data, &available); stream->s_in,
&data,
&available);
if (!FX_OK(status)) { if (!FX_OK(status)) {
break; break;
} }
@@ -128,7 +141,10 @@ static enum fx_status read_uncompressed(
} }
status = fx_stream_read_bytes( status = fx_stream_read_bytes(
stream->s_endpoint, dest, remaining, &nr_read_from_endpoint); stream->s_endpoint,
dest,
remaining,
&nr_read_from_endpoint);
stream->s_tx_bytes_uncompressed += nr_read_from_endpoint; stream->s_tx_bytes_uncompressed += nr_read_from_endpoint;
stream->s_tx_bytes += nr_read_from_endpoint; stream->s_tx_bytes += nr_read_from_endpoint;
@@ -150,14 +166,19 @@ static enum fx_status refill_input_buffer(struct fx_cstream_p *stream)
void *data; void *data;
size_t capacity; size_t capacity;
status = fx_ringbuffer_open_write_buffer( status = fx_ringbuffer_open_write_buffer(
stream->s_in, &data, &capacity); stream->s_in,
&data,
&capacity);
if (!FX_OK(status)) { if (!FX_OK(status)) {
break; break;
} }
size_t r = 0; size_t r = 0;
status = fx_stream_read_bytes( status = fx_stream_read_bytes(
stream->s_endpoint, data, capacity, &r); stream->s_endpoint,
data,
capacity,
&r);
fx_ringbuffer_close_write_buffer(stream->s_in, &data, r); fx_ringbuffer_close_write_buffer(stream->s_in, &data, r);
nr_read += r; nr_read += r;
@@ -196,9 +217,11 @@ static enum fx_status refill_output_buffer(struct fx_cstream_p *stream)
return status; return status;
} }
size_t bytes_before = fx_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); status = fx_compressor_step(stream->s_compressor);
size_t bytes_after = fx_ringbuffer_available_data_remaining(stream->s_in); 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_compressed += (bytes_before - bytes_after);
stream->s_tx_bytes += (bytes_before - bytes_after); stream->s_tx_bytes += (bytes_before - bytes_after);
@@ -207,7 +230,10 @@ static enum fx_status refill_output_buffer(struct fx_cstream_p *stream)
} }
static enum fx_status cstream_read( static enum fx_status cstream_read(
struct fx_cstream_p *stream, void *buf, size_t count, size_t *out_nr_read) struct fx_cstream_p *stream,
void *buf,
size_t count,
size_t *out_nr_read)
{ {
if (stream->s_mode != FX_COMPRESSOR_MODE_DECOMPRESS) { if (stream->s_mode != FX_COMPRESSOR_MODE_DECOMPRESS) {
return FX_ERR_BAD_STATE; return FX_ERR_BAD_STATE;
@@ -238,7 +264,9 @@ static enum fx_status cstream_read(
const void *data; const void *data;
size_t available; size_t available;
status = fx_ringbuffer_open_read_buffer( status = fx_ringbuffer_open_read_buffer(
stream->s_out, &data, &available); stream->s_out,
&data,
&available);
if (!FX_OK(status)) { if (!FX_OK(status)) {
break; break;
} }
@@ -267,13 +295,23 @@ static enum fx_status cstream_read(
} }
static enum fx_status write_cursor( static enum fx_status write_cursor(
struct fx_cstream_p *stream, const void *buf, size_t count, size_t *nr_written) struct fx_cstream_p *stream,
const void *buf,
size_t count,
size_t *nr_written)
{ {
return fx_stream_write_bytes(stream->s_endpoint, buf, count, nr_written); return fx_stream_write_bytes(
stream->s_endpoint,
buf,
count,
nr_written);
} }
static enum fx_status write_uncompressed( static enum fx_status write_uncompressed(
struct fx_cstream_p *stream, const void *buf, size_t count, size_t *nr_written) struct fx_cstream_p *stream,
const void *buf,
size_t count,
size_t *nr_written)
{ {
size_t w = 0; size_t w = 0;
enum fx_status status enum fx_status status
@@ -307,14 +345,19 @@ static enum fx_status flush_output_buffer(struct fx_cstream_p *stream)
const void *data; const void *data;
size_t capacity; size_t capacity;
status = fx_ringbuffer_open_read_buffer( status = fx_ringbuffer_open_read_buffer(
stream->s_out, &data, &capacity); stream->s_out,
&data,
&capacity);
if (!FX_OK(status)) { if (!FX_OK(status)) {
break; break;
} }
size_t w = 0; size_t w = 0;
status = fx_stream_write_bytes( status = fx_stream_write_bytes(
stream->s_endpoint, data, capacity, &w); stream->s_endpoint,
data,
capacity,
&w);
fx_ringbuffer_close_read_buffer(stream->s_out, &data, w); fx_ringbuffer_close_read_buffer(stream->s_out, &data, w);
nr_written += w; nr_written += w;
@@ -338,7 +381,9 @@ static enum fx_status flush_output_buffer(struct fx_cstream_p *stream)
} }
static enum fx_status cstream_write( static enum fx_status cstream_write(
struct fx_cstream_p *stream, const void *buf, size_t count, struct fx_cstream_p *stream,
const void *buf,
size_t count,
size_t *out_nr_written) size_t *out_nr_written)
{ {
if (stream->s_mode != FX_COMPRESSOR_MODE_COMPRESS) { if (stream->s_mode != FX_COMPRESSOR_MODE_COMPRESS) {
@@ -374,7 +419,9 @@ static enum fx_status cstream_write(
void *data; void *data;
size_t available; size_t available;
status = fx_ringbuffer_open_write_buffer( status = fx_ringbuffer_open_write_buffer(
stream->s_in, &data, &available); stream->s_in,
&data,
&available);
if (!FX_OK(status)) { if (!FX_OK(status)) {
break; break;
} }
@@ -403,7 +450,9 @@ static enum fx_status cstream_write(
} }
static enum fx_status skip_uncompressed( static enum fx_status skip_uncompressed(
struct fx_cstream_p *stream, size_t count, size_t *out_nr_skipped) struct fx_cstream_p *stream,
size_t count,
size_t *out_nr_skipped)
{ {
size_t remaining = count; size_t remaining = count;
size_t nr_read_from_buf = 0; size_t nr_read_from_buf = 0;
@@ -419,7 +468,9 @@ static enum fx_status skip_uncompressed(
const void *data; const void *data;
size_t available; size_t available;
status = fx_ringbuffer_open_read_buffer( status = fx_ringbuffer_open_read_buffer(
stream->s_in, &data, &available); stream->s_in,
&data,
&available);
if (!FX_OK(status)) { if (!FX_OK(status)) {
break; break;
} }
@@ -447,7 +498,10 @@ static enum fx_status skip_uncompressed(
size_t cursor = fx_stream_cursor(stream->s_endpoint); size_t cursor = fx_stream_cursor(stream->s_endpoint);
status = fx_stream_seek(stream->s_endpoint, remaining, FX_STREAM_SEEK_CURRENT); status = fx_stream_seek(
stream->s_endpoint,
remaining,
FX_STREAM_SEEK_CURRENT);
nr_read_from_endpoint = fx_stream_cursor(stream->s_endpoint) - cursor; nr_read_from_endpoint = fx_stream_cursor(stream->s_endpoint) - cursor;
stream->s_tx_bytes_uncompressed += nr_read_from_endpoint; stream->s_tx_bytes_uncompressed += nr_read_from_endpoint;
stream->s_tx_bytes += nr_read_from_endpoint; stream->s_tx_bytes += nr_read_from_endpoint;
@@ -460,7 +514,9 @@ static enum fx_status skip_uncompressed(
} }
static enum fx_status cstream_skip( static enum fx_status cstream_skip(
struct fx_cstream_p *stream, size_t count, size_t *out_nr_skipped) struct fx_cstream_p *stream,
size_t count,
size_t *out_nr_skipped)
{ {
if (stream->s_mode != FX_COMPRESSOR_MODE_DECOMPRESS) { if (stream->s_mode != FX_COMPRESSOR_MODE_DECOMPRESS) {
return FX_ERR_BAD_STATE; return FX_ERR_BAD_STATE;
@@ -499,7 +555,9 @@ static enum fx_status cstream_skip(
const void *data; const void *data;
size_t available; size_t available;
status = fx_ringbuffer_open_read_buffer( status = fx_ringbuffer_open_read_buffer(
stream->s_out, &data, &available); stream->s_out,
&data,
&available);
if (!FX_OK(status)) { if (!FX_OK(status)) {
break; break;
} }
@@ -554,7 +612,8 @@ static enum fx_status cstream_reset(struct fx_cstream_p *stream)
} }
static enum fx_status cstream_begin_compressed_section( static enum fx_status cstream_begin_compressed_section(
struct fx_cstream_p *stream, size_t *tx_uncompressed_bytes) struct fx_cstream_p *stream,
size_t *tx_uncompressed_bytes)
{ {
if (stream->s_flags & CSTREAM_CURSOR_MOVED) { if (stream->s_flags & CSTREAM_CURSOR_MOVED) {
return FX_ERR_BAD_STATE; return FX_ERR_BAD_STATE;
@@ -578,7 +637,8 @@ static enum fx_status cstream_begin_compressed_section(
} }
static enum fx_status cstream_end_compressed_section( static enum fx_status cstream_end_compressed_section(
struct fx_cstream_p *stream, size_t *tx_compressed_bytes, struct fx_cstream_p *stream,
size_t *tx_compressed_bytes,
size_t *tx_uncompressed_bytes) size_t *tx_uncompressed_bytes)
{ {
if (stream->s_flags & CSTREAM_CURSOR_MOVED) { if (stream->s_flags & CSTREAM_CURSOR_MOVED) {
@@ -646,28 +706,33 @@ static bool cstream_in_compressed_section(const struct fx_cstream_p *stream)
return stream->s_compression_depth > 0; return stream->s_compression_depth > 0;
} }
static enum fx_status cstream_tx_bytes(const struct fx_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; *out = stream->s_tx_bytes;
return FX_SUCCESS; return FX_SUCCESS;
} }
static enum fx_status cstream_tx_bytes_compressed( static enum fx_status cstream_tx_bytes_compressed(
const struct fx_cstream_p *stream, size_t *out) const struct fx_cstream_p *stream,
size_t *out)
{ {
*out = stream->s_tx_bytes_compressed; *out = stream->s_tx_bytes_compressed;
return FX_SUCCESS; return FX_SUCCESS;
} }
static enum fx_status cstream_tx_bytes_uncompressed( static enum fx_status cstream_tx_bytes_uncompressed(
const struct fx_cstream_p *stream, size_t *out) const struct fx_cstream_p *stream,
size_t *out)
{ {
*out = stream->s_tx_bytes_uncompressed; *out = stream->s_tx_bytes_uncompressed;
return FX_SUCCESS; return FX_SUCCESS;
} }
static enum fx_status cstream_set_cursor_position( static enum fx_status cstream_set_cursor_position(
struct fx_cstream_p *stream, size_t pos) struct fx_cstream_p *stream,
size_t pos)
{ {
if (stream->s_compression_depth > 0) { if (stream->s_compression_depth > 0) {
return FX_ERR_BAD_STATE; return FX_ERR_BAD_STATE;
@@ -691,14 +756,17 @@ static enum fx_status cstream_set_cursor_position(
return FX_SUCCESS; return FX_SUCCESS;
} }
static enum fx_status cstream_restore_cursor_position(struct fx_cstream_p *stream) static enum fx_status cstream_restore_cursor_position(
struct fx_cstream_p *stream)
{ {
if (!(stream->s_flags & CSTREAM_CURSOR_MOVED)) { if (!(stream->s_flags & CSTREAM_CURSOR_MOVED)) {
return FX_ERR_BAD_STATE; return FX_ERR_BAD_STATE;
} }
enum fx_status status = fx_stream_seek( enum fx_status status = fx_stream_seek(
stream->s_endpoint, stream->s_cursor, FX_STREAM_SEEK_START); stream->s_endpoint,
stream->s_cursor,
FX_STREAM_SEEK_START);
stream->s_cursor = 0; stream->s_cursor = 0;
if (!FX_OK(status)) { if (!FX_OK(status)) {
@@ -713,12 +781,17 @@ static enum fx_status cstream_restore_cursor_position(struct fx_cstream_p *strea
/*** PUBLIC FUNCTIONS *********************************************************/ /*** PUBLIC FUNCTIONS *********************************************************/
enum fx_status fx_cstream_open( enum fx_status fx_cstream_open(
fx_stream *endpoint, fx_type compressor_type, fx_compressor_mode mode, fx_stream *endpoint,
fx_type_id compressor_type,
fx_compressor_mode mode,
fx_cstream **out) fx_cstream **out)
{ {
size_t inbuf_size = 0, outbuf_size = 0; size_t inbuf_size = 0, outbuf_size = 0;
enum fx_status status = fx_compressor_get_buffer_size( enum fx_status status = fx_compressor_get_buffer_size(
compressor_type, mode, &inbuf_size, &outbuf_size); compressor_type,
mode,
&inbuf_size,
&outbuf_size);
if (!FX_OK(status)) { if (!FX_OK(status)) {
return status; return status;
} }
@@ -764,23 +837,46 @@ enum fx_status fx_cstream_open(
} }
enum fx_status fx_cstream_read( enum fx_status fx_cstream_read(
fx_cstream *stream, void *buf, size_t count, size_t *out_nr_read) fx_cstream *stream,
void *buf,
size_t count,
size_t *out_nr_read)
{ {
FX_CLASS_DISPATCH_STATIC( FX_CLASS_DISPATCH_STATIC(
FX_TYPE_CSTREAM, cstream_read, stream, buf, count, out_nr_read); FX_TYPE_CSTREAM,
cstream_read,
stream,
buf,
count,
out_nr_read);
} }
enum fx_status fx_cstream_write( enum fx_status fx_cstream_write(
fx_cstream *stream, const void *buf, size_t count, size_t *out_nr_written) fx_cstream *stream,
const void *buf,
size_t count,
size_t *out_nr_written)
{ {
FX_CLASS_DISPATCH_STATIC( FX_CLASS_DISPATCH_STATIC(
FX_TYPE_CSTREAM, cstream_write, stream, buf, count, out_nr_written); FX_TYPE_CSTREAM,
cstream_write,
stream,
buf,
count,
out_nr_written);
} }
enum fx_status fx_cstream_skip(fx_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)
{ {
FX_CLASS_DISPATCH_STATIC( FX_CLASS_DISPATCH_STATIC(
FX_TYPE_CSTREAM, cstream_skip, stream, count, out_nr_skipped); FX_TYPE_CSTREAM,
cstream_skip,
stream,
count,
out_nr_skipped);
} }
enum fx_status fx_cstream_reset(fx_cstream *stream) enum fx_status fx_cstream_reset(fx_cstream *stream)
@@ -789,54 +885,83 @@ enum fx_status fx_cstream_reset(fx_cstream *stream)
} }
enum fx_status fx_cstream_begin_compressed_section( enum fx_status fx_cstream_begin_compressed_section(
fx_cstream *stream, size_t *tx_uncompressed_bytes) fx_cstream *stream,
size_t *tx_uncompressed_bytes)
{ {
FX_CLASS_DISPATCH_STATIC( FX_CLASS_DISPATCH_STATIC(
FX_TYPE_CSTREAM, cstream_begin_compressed_section, stream, FX_TYPE_CSTREAM,
cstream_begin_compressed_section,
stream,
tx_uncompressed_bytes); tx_uncompressed_bytes);
} }
enum fx_status fx_cstream_end_compressed_section( enum fx_status fx_cstream_end_compressed_section(
fx_cstream *stream, size_t *tx_compressed_bytes, size_t *tx_uncompressed_bytes) fx_cstream *stream,
size_t *tx_compressed_bytes,
size_t *tx_uncompressed_bytes)
{ {
FX_CLASS_DISPATCH_STATIC( FX_CLASS_DISPATCH_STATIC(
FX_TYPE_CSTREAM, cstream_end_compressed_section, stream, FX_TYPE_CSTREAM,
tx_compressed_bytes, tx_uncompressed_bytes); cstream_end_compressed_section,
stream,
tx_compressed_bytes,
tx_uncompressed_bytes);
} }
bool fx_cstream_in_compressed_section(const fx_cstream *stream) bool fx_cstream_in_compressed_section(const fx_cstream *stream)
{ {
FX_CLASS_DISPATCH_STATIC_0( FX_CLASS_DISPATCH_STATIC_0(
FX_TYPE_CSTREAM, cstream_in_compressed_section, stream); FX_TYPE_CSTREAM,
cstream_in_compressed_section,
stream);
} }
enum fx_status fx_cstream_tx_bytes(const fx_cstream *stream, size_t *out) enum fx_status fx_cstream_tx_bytes(const fx_cstream *stream, size_t *out)
{ {
FX_CLASS_DISPATCH_STATIC(FX_TYPE_CSTREAM, cstream_tx_bytes, stream, out); FX_CLASS_DISPATCH_STATIC(
FX_TYPE_CSTREAM,
cstream_tx_bytes,
stream,
out);
} }
enum fx_status fx_cstream_tx_bytes_compressed(const fx_cstream *stream, size_t *out) enum fx_status fx_cstream_tx_bytes_compressed(
const fx_cstream *stream,
size_t *out)
{ {
FX_CLASS_DISPATCH_STATIC( FX_CLASS_DISPATCH_STATIC(
FX_TYPE_CSTREAM, cstream_tx_bytes_compressed, stream, out); FX_TYPE_CSTREAM,
cstream_tx_bytes_compressed,
stream,
out);
} }
enum fx_status fx_cstream_tx_bytes_uncompressed(const fx_cstream *stream, size_t *out) enum fx_status fx_cstream_tx_bytes_uncompressed(
const fx_cstream *stream,
size_t *out)
{ {
FX_CLASS_DISPATCH_STATIC( FX_CLASS_DISPATCH_STATIC(
FX_TYPE_CSTREAM, cstream_tx_bytes_uncompressed, stream, out); FX_TYPE_CSTREAM,
cstream_tx_bytes_uncompressed,
stream,
out);
} }
enum fx_status fx_cstream_set_cursor_position(fx_cstream *stream, size_t pos) enum fx_status fx_cstream_set_cursor_position(fx_cstream *stream, size_t pos)
{ {
FX_CLASS_DISPATCH_STATIC( FX_CLASS_DISPATCH_STATIC(
FX_TYPE_CSTREAM, cstream_set_cursor_position, stream, pos); FX_TYPE_CSTREAM,
cstream_set_cursor_position,
stream,
pos);
} }
enum fx_status fx_cstream_restore_cursor_position(fx_cstream *stream) enum fx_status fx_cstream_restore_cursor_position(fx_cstream *stream)
{ {
FX_CLASS_DISPATCH_STATIC_0( FX_CLASS_DISPATCH_STATIC_0(
FX_TYPE_CSTREAM, cstream_restore_cursor_position, stream); FX_TYPE_CSTREAM,
cstream_restore_cursor_position,
stream);
} }
/*** VIRTUAL FUNCTIONS ********************************************************/ /*** VIRTUAL FUNCTIONS ********************************************************/
@@ -864,12 +989,12 @@ static void cstream_fini(fx_object *obj, void *priv)
/*** CLASS DEFINITION *********************************************************/ /*** CLASS DEFINITION *********************************************************/
FX_TYPE_CLASS_DEFINITION_BEGIN(fx_cstream) FX_TYPE_CLASS_BEGIN(fx_cstream)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL; FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT) FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_stream, FX_TYPE_STREAM) FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_stream, FX_TYPE_STREAM)
FX_INTERFACE_ENTRY(s_close) = NULL; FX_INTERFACE_ENTRY(s_close) = NULL;
FX_INTERFACE_ENTRY(s_seek) = NULL; FX_INTERFACE_ENTRY(s_seek) = NULL;
FX_INTERFACE_ENTRY(s_tell) = NULL; FX_INTERFACE_ENTRY(s_tell) = NULL;
@@ -877,8 +1002,8 @@ FX_TYPE_CLASS_DEFINITION_BEGIN(fx_cstream)
FX_INTERFACE_ENTRY(s_read) = fx_cstream_read; FX_INTERFACE_ENTRY(s_read) = fx_cstream_read;
FX_INTERFACE_ENTRY(s_write) = fx_cstream_write; FX_INTERFACE_ENTRY(s_write) = fx_cstream_write;
FX_INTERFACE_ENTRY(s_reserve) = NULL; FX_INTERFACE_ENTRY(s_reserve) = NULL;
FX_TYPE_CLASS_INTERFACE_END(fx_stream, FX_TYPE_STREAM) FX_TYPE_VTABLE_INTERFACE_END(fx_stream, FX_TYPE_STREAM)
FX_TYPE_CLASS_DEFINITION_END(fx_cstream) FX_TYPE_CLASS_END(fx_cstream)
FX_TYPE_DEFINITION_BEGIN(fx_cstream) FX_TYPE_DEFINITION_BEGIN(fx_cstream)
FX_TYPE_ID(0xe1e899b5, 0x6a3c, 0x4f9c, 0xafd0, 0xaab3f156615c); FX_TYPE_ID(0xe1e899b5, 0x6a3c, 0x4f9c, 0xafd0, 0xaab3f156615c);
+44 -19
View File
@@ -1,5 +1,5 @@
#include <fx/compress/zstd.h> #include <fx/compression/zstd.h>
#include <fx/core/ringbuffer.h> #include <fx/ringbuffer.h>
#include <zstd.h> #include <zstd.h>
/*** PRIVATE DATA *************************************************************/ /*** PRIVATE DATA *************************************************************/
@@ -14,7 +14,9 @@ struct fx_zstd_compressor_p {
/*** PUBLIC FUNCTIONS *********************************************************/ /*** PUBLIC FUNCTIONS *********************************************************/
fx_status fx_zstd_compressor_get_buffer_size( fx_status fx_zstd_compressor_get_buffer_size(
fx_compressor_mode mode, size_t *inbuf_size, size_t *outbuf_size) fx_compressor_mode mode,
size_t *inbuf_size,
size_t *outbuf_size)
{ {
switch (mode) { switch (mode) {
case FX_COMPRESSOR_MODE_COMPRESS: case FX_COMPRESSOR_MODE_COMPRESS:
@@ -40,7 +42,8 @@ static void zstd_compressor_init(fx_object *obj, void *priv)
static void zstd_compressor_fini(fx_object *obj, void *priv) static void zstd_compressor_fini(fx_object *obj, void *priv)
{ {
fx_compressor_data *c = fx_object_get_protected(obj, FX_TYPE_COMPRESSOR); fx_compressor_data *c
= fx_object_get_protected(obj, FX_TYPE_COMPRESSOR);
struct fx_zstd_compressor_p *ctx = priv; struct fx_zstd_compressor_p *ctx = priv;
switch (c->c_mode) { switch (c->c_mode) {
case FX_COMPRESSOR_MODE_COMPRESS: case FX_COMPRESSOR_MODE_COMPRESS:
@@ -109,13 +112,18 @@ static enum fx_status compress(fx_compressor *compressor)
const void *in_buf = NULL; const void *in_buf = NULL;
void *out_buf = NULL; void *out_buf = NULL;
status = fx_ringbuffer_open_read_buffer(in, &in_buf, &in_available); status = fx_ringbuffer_open_read_buffer(
in,
&in_buf,
&in_available);
if (!FX_OK(status)) { if (!FX_OK(status)) {
break; break;
} }
status = fx_ringbuffer_open_write_buffer( status = fx_ringbuffer_open_write_buffer(
out, &out_buf, &out_capacity); out,
&out_buf,
&out_capacity);
if (!FX_OK(status)) { if (!FX_OK(status)) {
fx_ringbuffer_close_read_buffer(in, &in_buf, 0); fx_ringbuffer_close_read_buffer(in, &in_buf, 0);
break; break;
@@ -135,7 +143,10 @@ static enum fx_status compress(fx_compressor *compressor)
do { do {
size_t ret = ZSTD_compressStream2( size_t ret = ZSTD_compressStream2(
ctx->zstd_c, &z_out, &z_in, ZSTD_e_continue); ctx->zstd_c,
&z_out,
&z_in,
ZSTD_e_continue);
if (ZSTD_isError(ret)) { if (ZSTD_isError(ret)) {
status = FX_ERR_COMPRESSION_FAILURE; status = FX_ERR_COMPRESSION_FAILURE;
break; break;
@@ -178,7 +189,9 @@ static enum fx_status compress_end(fx_compressor *compressor)
void *out_buf = NULL; void *out_buf = NULL;
size_t out_capacity = 0; size_t out_capacity = 0;
status = fx_ringbuffer_open_write_buffer( status = fx_ringbuffer_open_write_buffer(
out, &out_buf, &out_capacity); out,
&out_buf,
&out_capacity);
if (!FX_OK(status)) { if (!FX_OK(status)) {
break; break;
} }
@@ -192,7 +205,10 @@ static enum fx_status compress_end(fx_compressor *compressor)
do { do {
size_t ret = ZSTD_compressStream2( size_t ret = ZSTD_compressStream2(
ctx->zstd_c, &z_out, &z_in, ZSTD_e_end); ctx->zstd_c,
&z_out,
&z_in,
ZSTD_e_end);
if (ZSTD_isError(ret)) { if (ZSTD_isError(ret)) {
status = FX_ERR_COMPRESSION_FAILURE; status = FX_ERR_COMPRESSION_FAILURE;
finished = true; finished = true;
@@ -240,13 +256,18 @@ static enum fx_status decompress(fx_compressor *compressor)
const void *in_buf = NULL; const void *in_buf = NULL;
void *out_buf = NULL; void *out_buf = NULL;
status = fx_ringbuffer_open_read_buffer(in, &in_buf, &in_available); status = fx_ringbuffer_open_read_buffer(
in,
&in_buf,
&in_available);
if (!FX_OK(status)) { if (!FX_OK(status)) {
break; break;
} }
status = fx_ringbuffer_open_write_buffer( status = fx_ringbuffer_open_write_buffer(
out, &out_buf, &out_capacity); out,
&out_buf,
&out_capacity);
if (!FX_OK(status)) { if (!FX_OK(status)) {
fx_ringbuffer_close_read_buffer(in, &in_buf, 0); fx_ringbuffer_close_read_buffer(in, &in_buf, 0);
break; break;
@@ -266,7 +287,9 @@ static enum fx_status decompress(fx_compressor *compressor)
do { do {
size_t ret = ZSTD_decompressStream( size_t ret = ZSTD_decompressStream(
ctx->zstd_d, &z_out, &z_in); ctx->zstd_d,
&z_out,
&z_in);
if (ZSTD_isError(ret)) { if (ZSTD_isError(ret)) {
status = FX_ERR_COMPRESSION_FAILURE; status = FX_ERR_COMPRESSION_FAILURE;
break; break;
@@ -292,7 +315,9 @@ static enum fx_status decompress(fx_compressor *compressor)
return status; return status;
} }
static enum fx_status set_mode(fx_compressor *compressor, fx_compressor_mode mode) static enum fx_status set_mode(
fx_compressor *compressor,
fx_compressor_mode mode)
{ {
struct fx_zstd_compressor_p *ctx struct fx_zstd_compressor_p *ctx
= fx_object_get_private(compressor, FX_TYPE_ZSTD_COMPRESSOR); = fx_object_get_private(compressor, FX_TYPE_ZSTD_COMPRESSOR);
@@ -336,12 +361,12 @@ static enum fx_status set_mode(fx_compressor *compressor, fx_compressor_mode mod
/*** CLASS DEFINITION *********************************************************/ /*** CLASS DEFINITION *********************************************************/
FX_TYPE_CLASS_DEFINITION_BEGIN(fx_zstd_compressor) FX_TYPE_CLASS_BEGIN(fx_zstd_compressor)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL; FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT) FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_compressor, FX_TYPE_COMPRESSOR) FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_compressor, FX_TYPE_COMPRESSOR)
FX_INTERFACE_ENTRY(c_buffer_size) FX_INTERFACE_ENTRY(c_buffer_size)
= fx_zstd_compressor_get_buffer_size; = fx_zstd_compressor_get_buffer_size;
FX_INTERFACE_ENTRY(c_compress) = compress; FX_INTERFACE_ENTRY(c_compress) = compress;
@@ -349,8 +374,8 @@ FX_TYPE_CLASS_DEFINITION_BEGIN(fx_zstd_compressor)
FX_INTERFACE_ENTRY(c_decompress) = decompress; FX_INTERFACE_ENTRY(c_decompress) = decompress;
FX_INTERFACE_ENTRY(c_reset) = reset; FX_INTERFACE_ENTRY(c_reset) = reset;
FX_INTERFACE_ENTRY(c_set_mode) = set_mode; FX_INTERFACE_ENTRY(c_set_mode) = set_mode;
FX_TYPE_CLASS_INTERFACE_END(fx_compressor, FX_TYPE_COMPRESSOR) FX_TYPE_VTABLE_INTERFACE_END(fx_compressor, FX_TYPE_COMPRESSOR)
FX_TYPE_CLASS_DEFINITION_END(fx_zstd_compressor) FX_TYPE_CLASS_END(fx_zstd_compressor)
FX_TYPE_DEFINITION_BEGIN(fx_zstd_compressor) FX_TYPE_DEFINITION_BEGIN(fx_zstd_compressor)
FX_TYPE_ID(0x51d437fc, 0xe789, 0x4105, 0xbac7, 0xe6b3f45df198); FX_TYPE_ID(0x51d437fc, 0xe789, 0x4105, 0xbac7, 0xe6b3f45df198);
@@ -1,10 +1,10 @@
#ifndef FX_COMPRESS_COMPRESSOR_H_ #ifndef FX_COMPRESS_COMPRESSOR_H_
#define FX_COMPRESS_COMPRESSOR_H_ #define FX_COMPRESS_COMPRESSOR_H_
#include <fx/core/macros.h> #include <fx/macros.h>
#include <fx/core/misc.h> #include <fx/misc.h>
#include <fx/core/ringbuffer.h> #include <fx/ringbuffer.h>
#include <fx/core/status.h> #include <fx/status.h>
#include <stdbool.h> #include <stdbool.h>
FX_DECLS_BEGIN; FX_DECLS_BEGIN;
@@ -38,7 +38,7 @@ typedef struct fx_compressor_data {
fx_ringbuffer *c_in, *c_out; fx_ringbuffer *c_in, *c_out;
} fx_compressor_data; } fx_compressor_data;
FX_API fx_type fx_compressor_get_type(void); FX_API fx_type_id fx_compressor_get_type(void);
#if 0 #if 0
FX_API fx_status fx_compressor_create( FX_API fx_status fx_compressor_create(
@@ -48,15 +48,21 @@ FX_API fx_status fx_compressor_create(
#endif #endif
FX_API fx_status fx_compressor_get_buffer_size( FX_API fx_status fx_compressor_get_buffer_size(
fx_type type, fx_compressor_mode mode, size_t *inbuf_size, fx_type_id type,
fx_compressor_mode mode,
size_t *inbuf_size,
size_t *outbuf_size); size_t *outbuf_size);
FX_API fx_status fx_compressor_get_mode( FX_API fx_status fx_compressor_get_mode(
const fx_compressor *compressor, fx_compressor_mode *out); const fx_compressor *compressor,
fx_compressor_mode *out);
FX_API fx_status fx_compressor_set_mode( FX_API fx_status fx_compressor_set_mode(
fx_compressor *compressor, fx_compressor_mode mode); fx_compressor *compressor,
fx_compressor_mode mode);
FX_API fx_status fx_compressor_set_buffer( FX_API fx_status fx_compressor_set_buffer(
fx_compressor *compressor, fx_ringbuffer *inbuf, fx_ringbuffer *outbuf); 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_step(fx_compressor *compressor);
FX_API fx_status fx_compressor_end(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 fx_status fx_compressor_reset(fx_compressor *compressor);
@@ -1,8 +1,8 @@
#ifndef FX_COMPRESS_CSTREAM_H_ #ifndef FX_COMPRESS_CSTREAM_H_
#define FX_COMPRESS_CSTREAM_H_ #define FX_COMPRESS_CSTREAM_H_
#include <fx/core/macros.h> #include <fx/macros.h>
#include <fx/core/stream.h> #include <fx/stream.h>
#include <stdbool.h> #include <stdbool.h>
FX_DECLS_BEGIN; FX_DECLS_BEGIN;
@@ -16,31 +16,40 @@ FX_DECLARE_TYPE(fx_cstream);
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_cstream) FX_TYPE_CLASS_DECLARATION_BEGIN(fx_cstream)
FX_TYPE_CLASS_DECLARATION_END(fx_cstream) FX_TYPE_CLASS_DECLARATION_END(fx_cstream)
FX_API fx_type fx_cstream_get_type(void); FX_API fx_type_id fx_cstream_get_type(void);
FX_API fx_status fx_cstream_open( FX_API fx_status fx_cstream_open(
fx_stream *endpoint, fx_type compressor_type, enum fx_compressor_mode mode, fx_stream *endpoint,
fx_type_id compressor_type,
enum fx_compressor_mode mode,
fx_cstream **out); fx_cstream **out);
FX_API fx_status fx_cstream_read( FX_API fx_status
fx_cstream *stream, void *buf, size_t count, size_t *nr_read); fx_cstream_read(fx_cstream *stream, void *buf, size_t count, size_t *nr_read);
FX_API fx_status fx_cstream_write( FX_API fx_status fx_cstream_write(
fx_cstream *stream, const void *buf, size_t count, size_t *nr_written); fx_cstream *stream,
FX_API fx_status fx_cstream_skip( const void *buf,
fx_cstream *stream, size_t count, size_t *nr_skipped); 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_reset(fx_cstream *stream);
FX_API fx_status fx_cstream_begin_compressed_section( FX_API fx_status fx_cstream_begin_compressed_section(
fx_cstream *stream, size_t *tx_uncompressed_bytes); fx_cstream *stream,
size_t *tx_uncompressed_bytes);
FX_API fx_status fx_cstream_end_compressed_section( FX_API fx_status fx_cstream_end_compressed_section(
fx_cstream *stream, size_t *tx_compressed_bytes, fx_cstream *stream,
size_t *tx_compressed_bytes,
size_t *tx_uncompressed_bytes); size_t *tx_uncompressed_bytes);
FX_API bool fx_cstream_in_compressed_section(const fx_cstream *stream); 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(const fx_cstream *stream, size_t *out);
FX_API fx_status fx_cstream_tx_bytes_compressed( FX_API fx_status fx_cstream_tx_bytes_compressed(
const fx_cstream *stream, size_t *out); const fx_cstream *stream,
size_t *out);
FX_API fx_status fx_cstream_tx_bytes_uncompressed( FX_API fx_status fx_cstream_tx_bytes_uncompressed(
const fx_cstream *stream, size_t *out); 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_set_cursor_position(fx_cstream *stream, size_t pos);
FX_API fx_status fx_cstream_restore_cursor_position(fx_cstream *stream); FX_API fx_status fx_cstream_restore_cursor_position(fx_cstream *stream);
@@ -1,10 +1,10 @@
#ifndef FX_COMPRESS_ZSTD_H_ #ifndef FX_COMPRESS_ZSTD_H_
#define FX_COMPRESS_ZSTD_H_ #define FX_COMPRESS_ZSTD_H_
#include <fx/compress/compressor.h> #include <fx/compression/compressor.h>
#include <fx/core/macros.h> #include <fx/macros.h>
#include <fx/core/misc.h> #include <fx/misc.h>
#include <fx/core/status.h> #include <fx/status.h>
#include <stdbool.h> #include <stdbool.h>
FX_DECLS_BEGIN; FX_DECLS_BEGIN;
@@ -16,10 +16,12 @@ FX_DECLARE_TYPE(fx_zstd_compressor);
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_zstd_compressor) FX_TYPE_CLASS_DECLARATION_BEGIN(fx_zstd_compressor)
FX_TYPE_CLASS_DECLARATION_END(fx_compressor) FX_TYPE_CLASS_DECLARATION_END(fx_compressor)
FX_API fx_type fx_zstd_compressor_get_type(void); FX_API fx_type_id fx_zstd_compressor_get_type(void);
FX_API fx_status fx_zstd_compressor_get_buffer_size( FX_API fx_status fx_zstd_compressor_get_buffer_size(
fx_compressor_mode mode, size_t *inbuf_size, size_t *outbuf_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_TYPE_DEFAULT_CONSTRUCTOR(fx_zstd_compressor, FX_TYPE_ZSTD_COMPRESSOR);
@@ -1,9 +1,9 @@
#include <assert.h> #include <assert.h>
#include <fx/compress/compressor.h> #include <fx/compression/compressor.h>
#include <fx/compress/cstream.h> #include <fx/compression/cstream.h>
#include <fx/compress/zstd.h> #include <fx/compression/zstd.h>
#include <fx/core/ringbuffer.h> #include <fx/ringbuffer.h>
#include <fx/core/stream.h> #include <fx/stream.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
@@ -48,9 +48,13 @@ int main(int argc, const char **argv)
size_t nr_written = 0; size_t nr_written = 0;
fx_status status = fx_cstream_write( fx_status status = fx_cstream_write(
cstream, source, source_len, &nr_written); cstream,
source,
source_len,
&nr_written);
if (!FX_OK(status)) { if (!FX_OK(status)) {
fprintf(stderr, "write error: %s\n", fprintf(stderr,
"write error: %s\n",
fx_status_description(status)); fx_status_description(status));
break; break;
} }
@@ -59,14 +63,19 @@ int main(int argc, const char **argv)
if (compressed) { if (compressed) {
fx_cstream_end_compressed_section( fx_cstream_end_compressed_section(
cstream, &nr_written_compressed, &nr_written); cstream,
&nr_written_compressed,
&nr_written);
} }
size_t tx_total = 0; size_t tx_total = 0;
fx_cstream_tx_bytes(cstream, &tx_total); fx_cstream_tx_bytes(cstream, &tx_total);
printf("iteration %d: wrote %zu (compressed) / %zu " printf("iteration %d: wrote %zu (compressed) / %zu "
"(uncompressed) / %zu (total) bytes (%s)\n", "(uncompressed) / %zu (total) bytes (%s)\n",
i, nr_written_compressed, nr_written, tx_total, i,
nr_written_compressed,
nr_written,
tx_total,
compressed ? "compressed" : "uncompressed"); compressed ? "compressed" : "uncompressed");
compressed = !compressed; compressed = !compressed;
@@ -1,9 +1,9 @@
#include <assert.h> #include <assert.h>
#include <fx/compress/compressor.h> #include <fx/compression/compressor.h>
#include <fx/compress/cstream.h> #include <fx/compression/cstream.h>
#include <fx/compress/zstd.h> #include <fx/compression/zstd.h>
#include <fx/core/ringbuffer.h> #include <fx/ringbuffer.h>
#include <fx/core/stream.h> #include <fx/stream.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
@@ -37,10 +37,14 @@ int main(int argc, const char **argv)
memset(buf, 0x0, sizeof buf); memset(buf, 0x0, sizeof buf);
size_t nr_read = 0; size_t nr_read = 0;
fx_status status fx_status status = fx_cstream_read(
= fx_cstream_read(cstream, buf, sizeof buf - 1, &nr_read); cstream,
buf,
sizeof buf - 1,
&nr_read);
if (!FX_OK(status)) { if (!FX_OK(status)) {
fprintf(stderr, "write error: %s\n", fprintf(stderr,
"write error: %s\n",
fx_status_description(status)); fx_status_description(status));
break; break;
} }
@@ -52,14 +56,19 @@ int main(int argc, const char **argv)
size_t nr_read_compressed = 0; size_t nr_read_compressed = 0;
if (compressed) { if (compressed) {
fx_cstream_end_compressed_section( fx_cstream_end_compressed_section(
cstream, &nr_read_compressed, &nr_read); cstream,
&nr_read_compressed,
&nr_read);
} }
size_t tx_total = 0; size_t tx_total = 0;
fx_cstream_tx_bytes(cstream, &tx_total); fx_cstream_tx_bytes(cstream, &tx_total);
printf(" * iteration %d: read %zu (compressed) / %zu " printf(" * iteration %d: read %zu (compressed) / %zu "
"(uncompressed) / %zu (total) bytes (%s)\n", "(uncompressed) / %zu (total) bytes (%s)\n",
i, nr_read_compressed, nr_read, tx_total, i,
nr_read_compressed,
nr_read,
tx_total,
compressed ? "compressed" : "uncompressed"); compressed ? "compressed" : "uncompressed");
printf("%s\n", buf); printf("%s\n", buf);
@@ -1,7 +1,7 @@
#include <assert.h> #include <assert.h>
#include <fx/compress/compressor.h> #include <fx/compression/compressor.h>
#include <fx/compress/zstd.h> #include <fx/compression/zstd.h>
#include <fx/core/ringbuffer.h> #include <fx/ringbuffer.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
@@ -12,8 +12,10 @@ int refill_input_buffer(FILE *fp, fx_ringbuffer *dest)
while (1) { while (1) {
void *buf; void *buf;
size_t capacity; size_t capacity;
fx_status status fx_status status = fx_ringbuffer_open_write_buffer(
= fx_ringbuffer_open_write_buffer(dest, &buf, &capacity); dest,
&buf,
&capacity);
if (status == FX_ERR_NO_SPACE) { if (status == FX_ERR_NO_SPACE) {
break; break;
} }
@@ -43,8 +45,10 @@ int flush_output_buffer(FILE *fp, fx_ringbuffer *src)
while (1) { while (1) {
const void *buf; const void *buf;
size_t capacity; size_t capacity;
fx_status status fx_status status = fx_ringbuffer_open_read_buffer(
= fx_ringbuffer_open_read_buffer(src, &buf, &capacity); src,
&buf,
&capacity);
if (status == FX_ERR_NO_DATA) { if (status == FX_ERR_NO_DATA) {
break; break;
} }
@@ -68,7 +72,9 @@ int flush_output_buffer(FILE *fp, fx_ringbuffer *src)
int main(int argc, const char **argv) int main(int argc, const char **argv)
{ {
if (argc < 4) { if (argc < 4) {
fprintf(stderr, "usage: %s <C/D> <inpath> <outpath>\n", argv[0]); fprintf(stderr,
"usage: %s <C/D> <inpath> <outpath>\n",
argv[0]);
return -1; return -1;
} }
@@ -96,12 +102,15 @@ int main(int argc, const char **argv)
} }
fx_status status = FX_SUCCESS; fx_status status = FX_SUCCESS;
fx_type compressor_type = FX_TYPE_ZSTD_COMPRESSOR; fx_type_id compressor_type = FX_TYPE_ZSTD_COMPRESSOR;
fx_compressor *compressor = fx_object_create(compressor_type); fx_compressor *compressor = fx_object_create(compressor_type);
size_t inbuf_size, outbuf_size; size_t inbuf_size, outbuf_size;
fx_compressor_get_buffer_size( fx_compressor_get_buffer_size(
compressor_type, mode, &inbuf_size, &outbuf_size); compressor_type,
mode,
&inbuf_size,
&outbuf_size);
fx_ringbuffer *in = fx_ringbuffer_create(inbuf_size); fx_ringbuffer *in = fx_ringbuffer_create(inbuf_size);
fx_ringbuffer *out = fx_ringbuffer_create(outbuf_size); fx_ringbuffer *out = fx_ringbuffer_create(outbuf_size);
@@ -1,21 +1,24 @@
#include <assert.h> #include <assert.h>
#include <fx/compress/compressor.h> #include <fx/compression/compressor.h>
#include <fx/compress/cstream.h> #include <fx/compression/cstream.h>
#include <fx/compress/zstd.h> #include <fx/compression/zstd.h>
#include <fx/core/ringbuffer.h> #include <fx/ringbuffer.h>
#include <fx/core/stream.h> #include <fx/stream.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#define BUF_SIZE 32 #define BUF_SIZE 32
static int compress(fx_type compressor_type, FILE *in, FILE *out) static int compress(fx_type_id compressor_type, FILE *in, FILE *out)
{ {
fx_stream *out_stream = fx_stream_open_fp(out); fx_stream *out_stream = fx_stream_open_fp(out);
fx_cstream *cstream; fx_cstream *cstream;
fx_status status = fx_cstream_open( fx_status status = fx_cstream_open(
out_stream, compressor_type, FX_COMPRESSOR_MODE_COMPRESS, &cstream); out_stream,
compressor_type,
FX_COMPRESSOR_MODE_COMPRESS,
&cstream);
if (!FX_OK(status)) { if (!FX_OK(status)) {
fprintf(stderr, "cannot initialise compressor\n"); fprintf(stderr, "cannot initialise compressor\n");
@@ -48,13 +51,16 @@ static int compress(fx_type compressor_type, FILE *in, FILE *out)
return 0; return 0;
} }
static int decompress(fx_type compressor_type, FILE *in, FILE *out) static int decompress(fx_type_id compressor_type, FILE *in, FILE *out)
{ {
fx_stream *in_stream = fx_stream_open_fp(in); fx_stream *in_stream = fx_stream_open_fp(in);
fx_cstream *cstream; fx_cstream *cstream;
fx_status status = fx_cstream_open( fx_status status = fx_cstream_open(
in_stream, compressor_type, FX_COMPRESSOR_MODE_DECOMPRESS, &cstream); in_stream,
compressor_type,
FX_COMPRESSOR_MODE_DECOMPRESS,
&cstream);
if (!FX_OK(status)) { if (!FX_OK(status)) {
fprintf(stderr, "cannot initialise compressor\n"); fprintf(stderr, "cannot initialise compressor\n");
@@ -66,9 +72,14 @@ static int decompress(fx_type compressor_type, FILE *in, FILE *out)
char buf[4096]; char buf[4096];
while (1) { while (1) {
size_t r = 0; size_t r = 0;
fx_status status = fx_cstream_read(cstream, buf, sizeof buf, &r); fx_status status = fx_cstream_read(
cstream,
buf,
sizeof buf,
&r);
if (!FX_OK(status)) { if (!FX_OK(status)) {
fprintf(stderr, "read error: %s\n", fprintf(stderr,
"read error: %s\n",
fx_status_description(status)); fx_status_description(status));
return -1; return -1;
} }
@@ -94,7 +105,9 @@ static int decompress(fx_type compressor_type, FILE *in, FILE *out)
int main(int argc, const char **argv) int main(int argc, const char **argv)
{ {
if (argc < 4) { if (argc < 4) {
fprintf(stderr, "usage: %s <C/D> <inpath> <outpath>\n", argv[0]); fprintf(stderr,
"usage: %s <C/D> <inpath> <outpath>\n",
argv[0]);
return -1; return -1;
} }
+1 -3
View File
@@ -1,3 +1 @@
include(../cmake/Templates.cmake) export_fx_namespace_details(fx.io)
add_fx_module(NAME io DEPENDENCIES core ds)
+24 -13
View File
@@ -1,12 +1,12 @@
#ifndef FX_IO_DIRECTORY_H_ #ifndef FX_IO_DIRECTORY_H_
#define FX_IO_DIRECTORY_H_ #define FX_IO_DIRECTORY_H_
#include <fx/core/error.h> #include <fx/error.h>
#include <fx/core/iterator.h>
#include <fx/core/macros.h>
#include <fx/core/misc.h>
#include <fx/io/file.h> #include <fx/io/file.h>
#include <fx/io/path.h> #include <fx/io/path.h>
#include <fx/iterator.h>
#include <fx/macros.h>
#include <fx/misc.h>
#define FX_DIRECTORY_ROOT ((fx_directory *)NULL) #define FX_DIRECTORY_ROOT ((fx_directory *)NULL)
@@ -45,11 +45,13 @@ typedef struct fx_directory_entry {
fx_file_info info; fx_file_info info;
} fx_directory_entry; } fx_directory_entry;
FX_API fx_type fx_directory_get_type(void); FX_API fx_type_id fx_directory_get_type(void);
FX_API fx_type fx_directory_iterator_get_type(void); FX_API fx_type_id fx_directory_iterator_get_type(void);
FX_API fx_result fx_directory_open( FX_API fx_result fx_directory_open(
fx_directory *root, const fx_path *path, fx_directory_open_flags flags, fx_directory *root,
const fx_path *path,
fx_directory_open_flags flags,
fx_directory **out); fx_directory **out);
FX_API fx_result fx_directory_open_temp(fx_directory **out); FX_API fx_result fx_directory_open_temp(fx_directory **out);
FX_API const fx_path *fx_directory_get_path(const fx_directory *dir); FX_API const fx_path *fx_directory_get_path(const fx_directory *dir);
@@ -58,17 +60,26 @@ FX_API const char *fx_directory_get_path_cstr(const fx_directory *dir);
FX_API const char *fx_directory_get_rel_path_cstr(const fx_directory *dir); FX_API const char *fx_directory_get_rel_path_cstr(const fx_directory *dir);
FX_API fx_result fx_directory_delete(fx_directory *dir); FX_API fx_result fx_directory_delete(fx_directory *dir);
FX_API bool fx_directory_path_exists(const fx_directory *root, const fx_path *path); FX_API bool fx_directory_path_exists(
FX_API bool fx_directory_path_is_file(const fx_directory *root, const fx_path *path); 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( FX_API bool fx_directory_path_is_directory(
const fx_directory *root, const fx_path *path); const fx_directory *root,
const fx_path *path);
FX_API fx_result fx_directory_path_stat( FX_API fx_result fx_directory_path_stat(
const fx_directory *root, const fx_path *path, struct fx_file_info *out); const fx_directory *root,
const fx_path *path,
struct fx_file_info *out);
FX_API fx_result fx_directory_path_unlink( FX_API fx_result fx_directory_path_unlink(
const fx_directory *root, const fx_path *path); const fx_directory *root,
const fx_path *path);
FX_API fx_iterator *fx_directory_begin( FX_API fx_iterator *fx_directory_begin(
fx_directory *dir, fx_directory_iterator_flags flags); fx_directory *dir,
fx_directory_iterator_flags flags);
FX_DECLS_END; FX_DECLS_END;
+22 -12
View File
@@ -1,10 +1,10 @@
#ifndef FX_IO_FILE_H_ #ifndef FX_IO_FILE_H_
#define FX_IO_FILE_H_ #define FX_IO_FILE_H_
#include <fx/core/error.h> #include <fx/error.h>
#include <fx/core/macros.h> #include <fx/macros.h>
#include <fx/core/misc.h> #include <fx/misc.h>
#include <fx/core/stream.h> #include <fx/stream.h>
FX_DECLS_BEGIN; FX_DECLS_BEGIN;
@@ -51,28 +51,38 @@ typedef struct fx_file_info {
size_t length; size_t length;
} fx_file_info; } fx_file_info;
FX_API fx_type fx_file_get_type(void); FX_API fx_type_id fx_file_get_type(void);
FX_API fx_result fx_file_open( FX_API fx_result fx_file_open(
FX_TYPE_FWDREF(fx_directory) * root, const FX_TYPE_FWDREF(fx_path) * path, FX_TYPE_FWDREF(fx_directory) * root,
fx_file_mode mode, fx_file **out); 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_temp(fx_file_mode mode, fx_file **out);
FX_API fx_result fx_file_open_shadow( FX_API fx_result
fx_file *original, fx_file_mode mode, fx_file **out); 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_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_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_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_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 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 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_swap_shadow(fx_file *main_file, fx_file *shadow_file);
FX_API fx_status fx_file_read( FX_API fx_status fx_file_read(
fx_file *file, size_t offset, size_t len, void *buf, size_t *nr_read); fx_file *file,
size_t offset,
size_t len,
void *buf,
size_t *nr_read);
FX_API fx_status fx_file_write( FX_API fx_status fx_file_write(
fx_file *file, size_t offset, size_t len, const void *buf, fx_file *file,
size_t offset,
size_t len,
const void *buf,
size_t *nr_written); size_t *nr_written);
FX_DECLS_END; FX_DECLS_END;
+10 -7
View File
@@ -1,10 +1,10 @@
#ifndef FX_IO_PATH_H_ #ifndef FX_IO_PATH_H_
#define FX_IO_PATH_H_ #define FX_IO_PATH_H_
#include <fx/core/macros.h> #include <fx/macros.h>
#include <fx/core/misc.h> #include <fx/misc.h>
#include <fx/core/status.h> #include <fx/status.h>
#include <fx/ds/string.h> #include <fx/string.h>
#include <stddef.h> #include <stddef.h>
FX_DECLS_BEGIN; FX_DECLS_BEGIN;
@@ -20,7 +20,7 @@ FX_TYPE_CLASS_DECLARATION_END(fx_path)
struct fx_file_info; struct fx_file_info;
FX_API fx_type fx_path_get_type(void); FX_API fx_type_id fx_path_get_type(void);
FX_TYPE_DEFAULT_CONSTRUCTOR(fx_path, FX_TYPE_PATH); FX_TYPE_DEFAULT_CONSTRUCTOR(fx_path, FX_TYPE_PATH);
@@ -43,8 +43,11 @@ 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 fx_status fx_path_unlink(const fx_path *path);
FX_API enum fx_status fx_path_get_directory( FX_API enum fx_status fx_path_get_directory(
const fx_path *path, fx_path **out_dir_path); const fx_path *path,
FX_API enum fx_status fx_path_get_filename(const fx_path *path, fx_string *out_name); 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 const char *fx_path_ptr(const fx_path *path);
FX_API size_t fx_path_length(const fx_path *path); FX_API size_t fx_path_length(const fx_path *path);
@@ -1,12 +1,12 @@
#include "misc.h" #include "misc.h"
#include "posix.h" #include "posix.h"
#include <fx/core/error.h>
#include <fx/ds/string.h>
#include <fx/io/directory.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <fts.h> #include <fts.h>
#include <fx/error.h>
#include <fx/io/directory.h>
#include <fx/string.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/stat.h> #include <sys/stat.h>
@@ -35,6 +35,7 @@ struct fx_directory_iterator_p {
fx_directory *root; fx_directory *root;
fx_directory_entry entry; fx_directory_entry entry;
fx_value entry_value;
}; };
/*** PRIVATE FUNCTIONS ********************************************************/ /*** PRIVATE FUNCTIONS ********************************************************/
@@ -58,11 +59,15 @@ static const char *directory_get_rel_path_cstr(const struct fx_directory_p *dir)
return fx_path_ptr(dir->d_path_rel); return fx_path_ptr(dir->d_path_rel);
} }
static fx_result directory_delete(fx_directory *dir, struct fx_directory_p *dir_p) static fx_result directory_delete(
fx_directory *dir,
struct fx_directory_p *dir_p)
{ {
enum fx_status status = FX_SUCCESS; enum fx_status status = FX_SUCCESS;
fx_iterator *it = fx_directory_begin(dir, FX_DIRECTORY_ITERATE_PARENT_LAST); fx_iterator *it = fx_directory_begin(
dir,
FX_DIRECTORY_ITERATE_PARENT_LAST);
while (FX_OK(fx_iterator_get_status(it))) { while (FX_OK(fx_iterator_get_status(it))) {
fx_iterator_erase(it); fx_iterator_erase(it);
} }
@@ -81,7 +86,8 @@ static fx_result directory_delete(fx_directory *dir, struct fx_directory_p *dir_
} }
static bool directory_path_exists( static bool directory_path_exists(
const struct fx_directory_p *root, const fx_path *path) const struct fx_directory_p *root,
const fx_path *path)
{ {
const fx_path *parts[] = { const fx_path *parts[] = {
root ? root->d_path_abs : NULL, root ? root->d_path_abs : NULL,
@@ -100,7 +106,8 @@ static bool directory_path_exists(
} }
static bool directory_path_is_file( static bool directory_path_is_file(
const struct fx_directory_p *root, const fx_path *path) const struct fx_directory_p *root,
const fx_path *path)
{ {
const fx_path *parts[] = { const fx_path *parts[] = {
root ? root->d_path_abs : NULL, root ? root->d_path_abs : NULL,
@@ -119,7 +126,8 @@ static bool directory_path_is_file(
} }
static bool directory_path_is_directory( static bool directory_path_is_directory(
const struct fx_directory_p *root, const fx_path *path) const struct fx_directory_p *root,
const fx_path *path)
{ {
const fx_path *parts[] = { const fx_path *parts[] = {
root ? root->d_path_abs : NULL, root ? root->d_path_abs : NULL,
@@ -138,7 +146,8 @@ static bool directory_path_is_directory(
} }
static fx_result directory_path_stat( static fx_result directory_path_stat(
const struct fx_directory_p *root, const fx_path *path, const struct fx_directory_p *root,
const fx_path *path,
struct fx_file_info *out) struct fx_file_info *out)
{ {
const fx_path *parts[] = { const fx_path *parts[] = {
@@ -158,7 +167,8 @@ static fx_result directory_path_stat(
} }
static fx_result directory_path_unlink( static fx_result directory_path_unlink(
const struct fx_directory_p *root, const fx_path *path) const struct fx_directory_p *root,
const fx_path *path)
{ {
const fx_path *parts[] = { const fx_path *parts[] = {
root ? root->d_path_abs : NULL, root ? root->d_path_abs : NULL,
@@ -192,11 +202,15 @@ static fx_result create_directory(struct fx_directory_p *root, const char *path)
} }
return fx_result_from_errno_with_subfilepath( return fx_result_from_errno_with_subfilepath(
errno, path, directory_get_rel_path_cstr(root), FX_ERR_IO_FAILURE); errno,
path,
directory_get_rel_path_cstr(root),
FX_ERR_IO_FAILURE);
} }
static fx_result create_directory_hierarchy( static fx_result create_directory_hierarchy(
struct fx_directory_p *root, const char *path) struct fx_directory_p *root,
const char *path)
{ {
int root_fd = root ? root->d_fd : AT_FDCWD; int root_fd = root ? root->d_fd : AT_FDCWD;
@@ -216,7 +230,9 @@ static fx_result create_directory_hierarchy(
int err = mkdirat(root_fd, path_buf, 0755); int err = mkdirat(root_fd, path_buf, 0755);
if (err != 0 && errno != EEXIST) { if (err != 0 && errno != EEXIST) {
result = fx_result_from_errno_with_subfilepath( result = fx_result_from_errno_with_subfilepath(
errno, path_buf, directory_get_rel_path_cstr(root), errno,
path_buf,
directory_get_rel_path_cstr(root),
FX_ERR_IO_FAILURE); FX_ERR_IO_FAILURE);
break; break;
} }
@@ -227,7 +243,9 @@ static fx_result create_directory_hierarchy(
int err = mkdirat(root_fd, path_buf, 0755); int err = mkdirat(root_fd, path_buf, 0755);
if (err != 0 && errno != EEXIST) { if (err != 0 && errno != EEXIST) {
result = fx_result_from_errno_with_subfilepath( result = fx_result_from_errno_with_subfilepath(
errno, path_buf, directory_get_rel_path_cstr(root), errno,
path_buf,
directory_get_rel_path_cstr(root),
FX_ERR_IO_FAILURE); FX_ERR_IO_FAILURE);
} }
@@ -236,8 +254,10 @@ static fx_result create_directory_hierarchy(
} }
static fx_result directory_open( static fx_result directory_open(
struct fx_directory_p *root, const fx_path *path, struct fx_directory_p *root,
fx_directory_open_flags flags, fx_directory **out) const fx_path *path,
fx_directory_open_flags flags,
fx_directory **out)
{ {
enum fx_status status = FX_SUCCESS; enum fx_status status = FX_SUCCESS;
@@ -254,7 +274,9 @@ static fx_result directory_open(
if ((flags & FX_DIRECTORY_OPEN_CREATE_INTERMEDIATE) if ((flags & FX_DIRECTORY_OPEN_CREATE_INTERMEDIATE)
== FX_DIRECTORY_OPEN_CREATE_INTERMEDIATE) { == FX_DIRECTORY_OPEN_CREATE_INTERMEDIATE) {
result = create_directory_hierarchy(root, path_cstr); result = create_directory_hierarchy(root, path_cstr);
} else if ((flags & FX_DIRECTORY_OPEN_CREATE) == FX_DIRECTORY_OPEN_CREATE) { } else if (
(flags & FX_DIRECTORY_OPEN_CREATE)
== FX_DIRECTORY_OPEN_CREATE) {
result = create_directory(root, path_cstr); result = create_directory(root, path_cstr);
} }
@@ -271,7 +293,9 @@ static fx_result directory_open(
fx_directory *dir = fx_object_create(FX_TYPE_DIRECTORY); fx_directory *dir = fx_object_create(FX_TYPE_DIRECTORY);
fx_path *cwd = NULL; fx_path *cwd = NULL;
struct fx_directory_p *p = fx_object_get_private(dir, FX_TYPE_DIRECTORY); struct fx_directory_p *p = fx_object_get_private(
dir,
FX_TYPE_DIRECTORY);
if (!root) { if (!root) {
cwd = fx_path_create_cwd(); cwd = fx_path_create_cwd();
@@ -307,11 +331,18 @@ static fx_result directory_open(
/*** PUBLIC FUNCTIONS *********************************************************/ /*** PUBLIC FUNCTIONS *********************************************************/
fx_result fx_directory_open( fx_result fx_directory_open(
fx_directory *root, const fx_path *path, fx_directory_open_flags flags, fx_directory *root,
const fx_path *path,
fx_directory_open_flags flags,
fx_directory **out) fx_directory **out)
{ {
FX_CLASS_DISPATCH_STATIC( FX_CLASS_DISPATCH_STATIC(
FX_TYPE_DIRECTORY, directory_open, root, path, flags, out); FX_TYPE_DIRECTORY,
directory_open,
root,
path,
flags,
out);
} }
fx_result fx_directory_open_temp(fx_directory **out) fx_result fx_directory_open_temp(fx_directory **out)
@@ -326,9 +357,13 @@ fx_result fx_directory_open_temp(fx_directory **out)
fx_directory *dir = NULL; fx_directory *dir = NULL;
fx_result status = fx_directory_open( fx_result status = fx_directory_open(
FX_DIRECTORY_ROOT, rpath, FX_DIRECTORY_OPEN_CREATE, &dir); FX_DIRECTORY_ROOT,
struct fx_directory_p *p rpath,
= fx_object_get_private(dir, FX_TYPE_DIRECTORY); FX_DIRECTORY_OPEN_CREATE,
&dir);
struct fx_directory_p *p = fx_object_get_private(
dir,
FX_TYPE_DIRECTORY);
if (fx_error_get_status_code(status) == FX_ERR_NAME_EXISTS) { if (fx_error_get_status_code(status) == FX_ERR_NAME_EXISTS) {
fx_path_unref(rpath); fx_path_unref(rpath);
@@ -341,8 +376,6 @@ fx_result fx_directory_open_temp(fx_directory **out)
fx_path_unlink(rpath); fx_path_unlink(rpath);
fx_path_unref(rpath); fx_path_unref(rpath);
return status;
} }
} }
@@ -353,23 +386,33 @@ const fx_path *fx_directory_get_path(const fx_directory *dir)
const fx_path *fx_directory_get_rel_path(const fx_directory *dir) const fx_path *fx_directory_get_rel_path(const fx_directory *dir)
{ {
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_DIRECTORY, directory_get_rel_path, dir); FX_CLASS_DISPATCH_STATIC_0(
FX_TYPE_DIRECTORY,
directory_get_rel_path,
dir);
} }
const char *fx_directory_get_path_cstr(const fx_directory *dir) const char *fx_directory_get_path_cstr(const fx_directory *dir)
{ {
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_DIRECTORY, directory_get_path_cstr, dir); FX_CLASS_DISPATCH_STATIC_0(
FX_TYPE_DIRECTORY,
directory_get_path_cstr,
dir);
} }
const char *fx_directory_get_rel_path_cstr(const fx_directory *dir) const char *fx_directory_get_rel_path_cstr(const fx_directory *dir)
{ {
FX_CLASS_DISPATCH_STATIC_0( FX_CLASS_DISPATCH_STATIC_0(
FX_TYPE_DIRECTORY, directory_get_rel_path_cstr, dir); FX_TYPE_DIRECTORY,
directory_get_rel_path_cstr,
dir);
} }
fx_result fx_directory_delete(fx_directory *dir) fx_result fx_directory_delete(fx_directory *dir)
{ {
struct fx_directory_p *p = fx_object_get_private(dir, FX_TYPE_DIRECTORY); struct fx_directory_p *p = fx_object_get_private(
dir,
FX_TYPE_DIRECTORY);
p->d_flags |= DIRECTORY_DELETE_ON_CLOSE; p->d_flags |= DIRECTORY_DELETE_ON_CLOSE;
/* TODO allow object release functions to return a fx_result value */ /* TODO allow object release functions to return a fx_result value */
fx_directory_unref(dir); fx_directory_unref(dir);
@@ -378,31 +421,55 @@ fx_result fx_directory_delete(fx_directory *dir)
bool fx_directory_path_exists(const fx_directory *root, const fx_path *path) bool fx_directory_path_exists(const fx_directory *root, const fx_path *path)
{ {
FX_CLASS_DISPATCH_STATIC(FX_TYPE_DIRECTORY, directory_path_exists, root, path); FX_CLASS_DISPATCH_STATIC(
FX_TYPE_DIRECTORY,
directory_path_exists,
root,
path);
} }
bool fx_directory_path_is_file(const fx_directory *root, const fx_path *path) bool fx_directory_path_is_file(const fx_directory *root, const fx_path *path)
{ {
FX_CLASS_DISPATCH_STATIC( FX_CLASS_DISPATCH_STATIC(
FX_TYPE_DIRECTORY, directory_path_is_file, root, path); FX_TYPE_DIRECTORY,
directory_path_is_file,
root,
path);
} }
bool fx_directory_path_is_directory(const fx_directory *root, const fx_path *path) bool fx_directory_path_is_directory(
const fx_directory *root,
const fx_path *path)
{ {
FX_CLASS_DISPATCH_STATIC( FX_CLASS_DISPATCH_STATIC(
FX_TYPE_DIRECTORY, directory_path_is_directory, root, path); FX_TYPE_DIRECTORY,
directory_path_is_directory,
root,
path);
} }
fx_result fx_directory_path_stat( fx_result fx_directory_path_stat(
const fx_directory *root, const fx_path *path, struct fx_file_info *out) const fx_directory *root,
const fx_path *path,
struct fx_file_info *out)
{ {
FX_CLASS_DISPATCH_STATIC( FX_CLASS_DISPATCH_STATIC(
FX_TYPE_DIRECTORY, directory_path_stat, root, path, out); FX_TYPE_DIRECTORY,
directory_path_stat,
root,
path,
out);
} }
fx_result fx_directory_path_unlink(const fx_directory *root, const fx_path *path) fx_result fx_directory_path_unlink(
const fx_directory *root,
const fx_path *path)
{ {
FX_CLASS_DISPATCH_STATIC(FX_TYPE_DIRECTORY, directory_path_unlink, root, path); FX_CLASS_DISPATCH_STATIC(
FX_TYPE_DIRECTORY,
directory_path_unlink,
root,
path);
} }
/*** VIRTUAL FUNCTIONS ********************************************************/ /*** VIRTUAL FUNCTIONS ********************************************************/
@@ -444,6 +511,8 @@ static void update_iterator_data(struct fx_directory_iterator_p *it)
fx_path *path = fx_path_create_from_cstr( fx_path *path = fx_path_create_from_cstr(
ent->fts_path + fx_path_length(it->_p->d_path_abs) + 1); ent->fts_path + fx_path_length(it->_p->d_path_abs) + 1);
fx_value_unset(&it->entry_value);
it->entry_value = FX_POINTER(&it->entry);
it->entry.filename = ent->fts_name; it->entry.filename = ent->fts_name;
it->entry.filepath = path; it->entry.filepath = path;
@@ -487,11 +556,13 @@ static void iterator_fini(fx_object *obj, void *priv)
} }
fx_iterator *fx_directory_begin( fx_iterator *fx_directory_begin(
fx_directory *directory, enum fx_directory_iterator_flags flags) fx_directory *directory,
enum fx_directory_iterator_flags flags)
{ {
fx_iterator *it_obj = fx_object_create(FX_TYPE_DIRECTORY_ITERATOR); fx_iterator *it_obj = fx_object_create(FX_TYPE_DIRECTORY_ITERATOR);
struct fx_directory_iterator_p *it struct fx_directory_iterator_p *it = fx_object_get_private(
= fx_object_get_private(it_obj, FX_TYPE_DIRECTORY_ITERATOR); it_obj,
FX_TYPE_DIRECTORY_ITERATOR);
it->flags = flags; it->flags = flags;
it->root = directory; it->root = directory;
@@ -549,20 +620,18 @@ fx_iterator *fx_directory_begin(
return it_obj; return it_obj;
} }
static fx_iterator *iterator_begin(fx_object *obj) static const fx_iterator *iterator_begin(const fx_object *obj)
{ {
return fx_directory_begin(obj, FX_DIRECTORY_ITERATE_PARENT_FIRST); return fx_directory_begin(
} (fx_object *)obj,
FX_DIRECTORY_ITERATE_PARENT_FIRST);
static const fx_iterator *iterator_cbegin(const fx_object *obj)
{
return fx_directory_begin((fx_object *)obj, FX_DIRECTORY_ITERATE_PARENT_FIRST);
} }
static enum fx_status iterator_move_next(const fx_iterator *obj) static enum fx_status iterator_move_next(const fx_iterator *obj)
{ {
struct fx_directory_iterator_p *it struct fx_directory_iterator_p *it = fx_object_get_private(
= fx_object_get_private(obj, FX_TYPE_DIRECTORY_ITERATOR); obj,
FX_TYPE_DIRECTORY_ITERATOR);
if (!it || !it->fts) { if (!it || !it->fts) {
return FX_ERR_NO_DATA; return FX_ERR_NO_DATA;
} }
@@ -610,9 +679,12 @@ static enum fx_status iterator_move_next(const fx_iterator *obj)
static enum fx_status iterator_erase(fx_iterator *obj) static enum fx_status iterator_erase(fx_iterator *obj)
{ {
struct fx_directory_iterator_p *it struct fx_directory_iterator_p *it = fx_object_get_private(
= fx_object_get_private(obj, FX_TYPE_DIRECTORY_ITERATOR); obj,
fx_result result = fx_directory_path_unlink(it->root, it->entry.filepath); FX_TYPE_DIRECTORY_ITERATOR);
fx_result result = fx_directory_path_unlink(
it->root,
it->entry.filepath);
if (fx_result_is_error(result)) { if (fx_result_is_error(result)) {
enum fx_status status = fx_error_get_status_code(result); enum fx_status status = fx_error_get_status_code(result);
fx_error_discard(result); fx_error_discard(result);
@@ -622,35 +694,27 @@ static enum fx_status iterator_erase(fx_iterator *obj)
return iterator_move_next(obj); return iterator_move_next(obj);
} }
static fx_iterator_value iterator_get_value(fx_iterator *obj) static const fx_value *iterator_get_value(const fx_iterator *obj)
{ {
struct fx_directory_iterator_p *it struct fx_directory_iterator_p *it = fx_object_get_private(
= fx_object_get_private(obj, FX_TYPE_DIRECTORY_ITERATOR); obj,
FX_TYPE_DIRECTORY_ITERATOR);
return FX_ITERATOR_VALUE_PTR(&it->entry); return &it->entry_value;
}
static const fx_iterator_value iterator_get_cvalue(const fx_iterator *obj)
{
struct fx_directory_iterator_p *it
= fx_object_get_private(obj, FX_TYPE_DIRECTORY_ITERATOR);
return FX_ITERATOR_VALUE_CPTR(&it->entry);
} }
/*** CLASS DEFINITION *********************************************************/ /*** CLASS DEFINITION *********************************************************/
// ---- fx_directory DEFINITION // ---- fx_directory DEFINITION
FX_TYPE_CLASS_DEFINITION_BEGIN(fx_directory) FX_TYPE_CLASS_BEGIN(fx_directory)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL; FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT) FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_iterable, FX_TYPE_ITERABLE) FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterable, FX_TYPE_ITERABLE)
FX_INTERFACE_ENTRY(it_begin) = iterator_begin; FX_INTERFACE_ENTRY(it_begin) = iterator_begin;
FX_INTERFACE_ENTRY(it_cbegin) = iterator_cbegin; FX_TYPE_VTABLE_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE)
FX_TYPE_CLASS_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE) FX_TYPE_CLASS_END(fx_directory)
FX_TYPE_CLASS_DEFINITION_END(fx_directory)
FX_TYPE_DEFINITION_BEGIN(fx_directory) FX_TYPE_DEFINITION_BEGIN(fx_directory)
FX_TYPE_ID(0x10d36546, 0x7f96, 0x464b, 0xbc4d, 0xe504b283fa45); FX_TYPE_ID(0x10d36546, 0x7f96, 0x464b, 0xbc4d, 0xe504b283fa45);
@@ -662,18 +726,17 @@ FX_TYPE_DEFINITION_BEGIN(fx_directory)
FX_TYPE_DEFINITION_END(fx_directory) FX_TYPE_DEFINITION_END(fx_directory)
// ---- fx_directory_iterator DEFINITION // ---- fx_directory_iterator DEFINITION
FX_TYPE_CLASS_DEFINITION_BEGIN(fx_directory_iterator) FX_TYPE_CLASS_BEGIN(fx_directory_iterator)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL; FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT) FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_iterator, FX_TYPE_ITERATOR) FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterator, FX_TYPE_ITERATOR)
FX_INTERFACE_ENTRY(it_move_next) = iterator_move_next; FX_INTERFACE_ENTRY(it_move_next) = iterator_move_next;
FX_INTERFACE_ENTRY(it_erase) = iterator_erase; FX_INTERFACE_ENTRY(it_erase) = iterator_erase;
FX_INTERFACE_ENTRY(it_get_value) = iterator_get_value; FX_INTERFACE_ENTRY(it_get_value) = iterator_get_value;
FX_INTERFACE_ENTRY(it_get_cvalue) = iterator_get_cvalue; FX_TYPE_VTABLE_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR)
FX_TYPE_CLASS_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR) FX_TYPE_CLASS_END(fx_directory_iterator)
FX_TYPE_CLASS_DEFINITION_END(fx_directory_iterator)
FX_TYPE_DEFINITION_BEGIN(fx_directory_iterator) FX_TYPE_DEFINITION_BEGIN(fx_directory_iterator)
FX_TYPE_ID(0xc707fce6, 0xc895, 0x4925, 0x8700, 0xa60641dee0cc); FX_TYPE_ID(0xc707fce6, 0xc895, 0x4925, 0x8700, 0xa60641dee0cc);
@@ -1,13 +1,13 @@
#include "misc.h" #include "misc.h"
#include "posix.h" #include "posix.h"
#include <fx/core/random.h> #include <errno.h>
#include <fx/ds/string.h> #include <fcntl.h>
#include <fx/io/directory.h> #include <fx/io/directory.h>
#include <fx/io/file.h> #include <fx/io/file.h>
#include <fx/io/path.h> #include <fx/io/path.h>
#include <errno.h> #include <fx/random.h>
#include <fcntl.h> #include <fx/string.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@@ -20,7 +20,10 @@ static enum fx_status stream_close(fx_stream *);
static enum fx_status stream_getc(fx_stream *, int *); 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_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_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_seek(
fx_stream *,
long long,
fx_stream_seek_origin);
static enum fx_status stream_tell(const fx_stream *, size_t *); static enum fx_status stream_tell(const fx_stream *, size_t *);
/*** PRIVATE DATA *************************************************************/ /*** PRIVATE DATA *************************************************************/
@@ -63,7 +66,9 @@ static unsigned int fx_mode_to_unix_mode(enum fx_file_mode mode)
} }
static fx_result file_open_shadow( static fx_result file_open_shadow(
struct fx_file_p *original, enum fx_file_mode mode, fx_file **out) struct fx_file_p *original,
enum fx_file_mode mode,
fx_file **out)
{ {
mode |= FX_FILE_SHADOW | FX_FILE_DELETE_ON_CLOSE | FX_FILE_CREATE; mode |= FX_FILE_SHADOW | FX_FILE_DELETE_ON_CLOSE | FX_FILE_CREATE;
@@ -75,7 +80,8 @@ static fx_result file_open_shadow(
fx_string_prepend_cstr(filename, ".~"); fx_string_prepend_cstr(filename, ".~");
fx_path *shadow_filename = fx_path_create_from_cstr(fx_string_get_cstr(filename)); fx_path *shadow_filename
= fx_path_create_from_cstr(fx_string_get_cstr(filename));
fx_string_unref(filename); fx_string_unref(filename);
const fx_path *parts[] = { const fx_path *parts[] = {
@@ -94,7 +100,10 @@ static fx_result file_open_shadow(
fx_file *shadow_file; fx_file *shadow_file;
fx_result status = fx_file_open( fx_result status = fx_file_open(
FX_DIRECTORY_ROOT, shadow_filepath, mode, &shadow_file); FX_DIRECTORY_ROOT,
shadow_filepath,
mode,
&shadow_file);
fx_path_unref(shadow_filepath); fx_path_unref(shadow_filepath);
if (fx_result_is_error(status)) { if (fx_result_is_error(status)) {
@@ -110,7 +119,9 @@ static const fx_path *file_path(const struct fx_file_p *file)
return file->path; return file->path;
} }
static enum fx_status file_stat(struct fx_file_p *file, struct fx_file_info *out) static enum fx_status file_stat(
struct fx_file_p *file,
struct fx_file_info *out)
{ {
struct stat st; struct stat st;
int err = fstat(file->fd, &st); int err = fstat(file->fd, &st);
@@ -167,7 +178,9 @@ static enum fx_status file_resize(struct fx_file_p *file, size_t len)
} }
static enum fx_status file_seek( static enum fx_status file_seek(
struct fx_file_p *file, long long offset, enum fx_seek_basis basis) struct fx_file_p *file,
long long offset,
enum fx_seek_basis basis)
{ {
int whence; int whence;
switch (basis) { switch (basis) {
@@ -193,7 +206,8 @@ static enum fx_status file_seek(
} }
static enum fx_status file_swap_shadow( static enum fx_status file_swap_shadow(
struct fx_file_p *main_file, struct fx_file_p *shadow_file) struct fx_file_p *main_file,
struct fx_file_p *shadow_file)
{ {
if (main_file->mode & FX_FILE_SHADOW) { if (main_file->mode & FX_FILE_SHADOW) {
return FX_ERR_NOT_SUPPORTED; return FX_ERR_NOT_SUPPORTED;
@@ -235,7 +249,9 @@ static enum fx_status file_swap_shadow(
int err; int err;
err = rename(fx_path_ptr(main_file->path), fx_path_ptr(tmp_path)); err = rename(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(shadow_file->path),
fx_path_ptr(main_file->path));
err = rename(fx_path_ptr(tmp_path), fx_path_ptr(shadow_file->path)); err = rename(fx_path_ptr(tmp_path), fx_path_ptr(shadow_file->path));
fx_path_unref(tmp_path); fx_path_unref(tmp_path);
@@ -248,7 +264,11 @@ static enum fx_status file_swap_shadow(
} }
static enum fx_status file_read( static enum fx_status file_read(
struct fx_file_p *file, size_t offset, size_t len, void *buf, size_t *nr_read) struct fx_file_p *file,
size_t offset,
size_t len,
void *buf,
size_t *nr_read)
{ {
if (offset != FX_OFFSET_CURRENT) { if (offset != FX_OFFSET_CURRENT) {
lseek(file->fd, offset, SEEK_SET); lseek(file->fd, offset, SEEK_SET);
@@ -267,7 +287,10 @@ static enum fx_status file_read(
} }
static enum fx_status file_write( static enum fx_status file_write(
struct fx_file_p *file, size_t offset, size_t len, const void *buf, struct fx_file_p *file,
size_t offset,
size_t len,
const void *buf,
size_t *nr_written) size_t *nr_written)
{ {
if (offset != FX_OFFSET_CURRENT) { if (offset != FX_OFFSET_CURRENT) {
@@ -314,7 +337,10 @@ static enum fx_status stream_getc(fx_stream *stream, int *out)
} }
static enum fx_status stream_read( static enum fx_status stream_read(
fx_stream *stream, void *buf, size_t max, size_t *nr_read) fx_stream *stream,
void *buf,
size_t max,
size_t *nr_read)
{ {
struct fx_file_p *file = fx_object_get_private(stream, FX_TYPE_FILE); struct fx_file_p *file = fx_object_get_private(stream, FX_TYPE_FILE);
@@ -325,7 +351,10 @@ static enum fx_status stream_read(
} }
static enum fx_status stream_write( static enum fx_status stream_write(
fx_stream *stream, const void *buf, size_t count, size_t *nr_written) fx_stream *stream,
const void *buf,
size_t count,
size_t *nr_written)
{ {
struct fx_file_p *file = fx_object_get_private(stream, FX_TYPE_FILE); struct fx_file_p *file = fx_object_get_private(stream, FX_TYPE_FILE);
@@ -336,7 +365,9 @@ static enum fx_status stream_write(
} }
static enum fx_status stream_seek( static enum fx_status stream_seek(
fx_stream *stream, long long offset, fx_stream_seek_origin origin) fx_stream *stream,
long long offset,
fx_stream_seek_origin origin)
{ {
fx_seek_basis basis; fx_seek_basis basis;
switch (origin) { switch (origin) {
@@ -360,7 +391,8 @@ static enum fx_status stream_seek(
static enum fx_status stream_tell(const fx_stream *stream, size_t *pos) static enum fx_status stream_tell(const fx_stream *stream, size_t *pos)
{ {
const struct fx_file_p *file = fx_object_get_private(stream, FX_TYPE_FILE); const struct fx_file_p *file
= fx_object_get_private(stream, FX_TYPE_FILE);
off_t v = lseek(file->fd, 0, SEEK_CUR); off_t v = lseek(file->fd, 0, SEEK_CUR);
if (v == (off_t)-1) { if (v == (off_t)-1) {
return fx_status_from_errno(errno, FX_ERR_IO_FAILURE); return fx_status_from_errno(errno, FX_ERR_IO_FAILURE);
@@ -373,7 +405,10 @@ static enum fx_status stream_tell(const fx_stream *stream, size_t *pos)
/*** PUBLIC FUNCTIONS *********************************************************/ /*** PUBLIC FUNCTIONS *********************************************************/
fx_result fx_file_open( fx_result fx_file_open(
fx_directory *root, const fx_path *path, enum fx_file_mode mode, fx_file **out) fx_directory *root,
const fx_path *path,
enum fx_file_mode mode,
fx_file **out)
{ {
const fx_path *file_path = path; const fx_path *file_path = path;
unsigned int flags = fx_mode_to_unix_mode(mode); unsigned int flags = fx_mode_to_unix_mode(mode);
@@ -456,7 +491,10 @@ fx_result fx_file_open_temp(enum fx_file_mode mode, fx_file **out)
fx_path *rpath = fx_path_create_from_cstr(path); fx_path *rpath = fx_path_create_from_cstr(path);
fx_result status = fx_file_open( fx_result status = fx_file_open(
FX_DIRECTORY_ROOT, rpath, mode | FX_FILE_CREATE_ONLY, out); FX_DIRECTORY_ROOT,
rpath,
mode | FX_FILE_CREATE_ONLY,
out);
if (fx_error_get_status_code(status) == FX_ERR_NAME_EXISTS) { if (fx_error_get_status_code(status) == FX_ERR_NAME_EXISTS) {
fx_path_unref(rpath); fx_path_unref(rpath);
@@ -470,9 +508,17 @@ fx_result fx_file_open_temp(enum fx_file_mode mode, fx_file **out)
} }
} }
fx_result fx_file_open_shadow(fx_file *original, enum fx_file_mode mode, fx_file **out) fx_result fx_file_open_shadow(
fx_file *original,
enum fx_file_mode mode,
fx_file **out)
{ {
FX_CLASS_DISPATCH_STATIC(FX_TYPE_FILE, file_open_shadow, original, mode, out); FX_CLASS_DISPATCH_STATIC(
FX_TYPE_FILE,
file_open_shadow,
original,
mode,
out);
} }
const fx_path *fx_file_path(const fx_file *file) const fx_path *fx_file_path(const fx_file *file)
@@ -500,30 +546,55 @@ enum fx_status fx_file_resize(fx_file *file, size_t len)
FX_CLASS_DISPATCH_STATIC(FX_TYPE_FILE, file_resize, file, len); FX_CLASS_DISPATCH_STATIC(FX_TYPE_FILE, file_resize, file, len);
} }
enum fx_status fx_file_seek(fx_file *file, long long offset, enum fx_seek_basis basis) enum fx_status fx_file_seek(
fx_file *file,
long long offset,
enum fx_seek_basis basis)
{ {
FX_CLASS_DISPATCH_STATIC(FX_TYPE_FILE, file_seek, file, offset, basis); FX_CLASS_DISPATCH_STATIC(FX_TYPE_FILE, file_seek, file, offset, basis);
} }
enum fx_status fx_file_swap_shadow(fx_file *main_file, fx_file *shadow_file) enum fx_status fx_file_swap_shadow(fx_file *main_file, fx_file *shadow_file)
{ {
struct fx_file_p *main_p = fx_object_get_private(main_file, FX_TYPE_FILE); struct fx_file_p *main_p
struct fx_file_p *shadow_p = fx_object_get_private(main_file, FX_TYPE_FILE); = 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); return file_swap_shadow(main_p, shadow_p);
} }
enum fx_status fx_file_read( enum fx_status fx_file_read(
fx_file *file, size_t offset, size_t len, void *buf, size_t *nr_read) fx_file *file,
size_t offset,
size_t len,
void *buf,
size_t *nr_read)
{ {
FX_CLASS_DISPATCH_STATIC( FX_CLASS_DISPATCH_STATIC(
FX_TYPE_FILE, file_read, file, offset, len, buf, nr_read); FX_TYPE_FILE,
file_read,
file,
offset,
len,
buf,
nr_read);
} }
enum fx_status fx_file_write( enum fx_status fx_file_write(
fx_file *file, size_t offset, size_t len, const void *buf, size_t *nr_written) fx_file *file,
size_t offset,
size_t len,
const void *buf,
size_t *nr_written)
{ {
FX_CLASS_DISPATCH_STATIC( FX_CLASS_DISPATCH_STATIC(
FX_TYPE_FILE, file_write, file, offset, len, buf, nr_written); FX_TYPE_FILE,
file_write,
file,
offset,
len,
buf,
nr_written);
} }
/*** VIRTUAL FUNCTIONS ********************************************************/ /*** VIRTUAL FUNCTIONS ********************************************************/
@@ -547,20 +618,20 @@ static void file_fini(fx_object *obj, void *priv)
/*** CLASS DEFINITION *********************************************************/ /*** CLASS DEFINITION *********************************************************/
FX_TYPE_CLASS_DEFINITION_BEGIN(fx_file) FX_TYPE_CLASS_BEGIN(fx_file)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL; FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT) FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_stream, FX_TYPE_STREAM) FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_stream, FX_TYPE_STREAM)
FX_INTERFACE_ENTRY(s_close) = stream_close; FX_INTERFACE_ENTRY(s_close) = stream_close;
FX_INTERFACE_ENTRY(s_getc) = stream_getc; FX_INTERFACE_ENTRY(s_getc) = stream_getc;
FX_INTERFACE_ENTRY(s_read) = stream_read; FX_INTERFACE_ENTRY(s_read) = stream_read;
FX_INTERFACE_ENTRY(s_write) = stream_write; FX_INTERFACE_ENTRY(s_write) = stream_write;
FX_INTERFACE_ENTRY(s_seek) = stream_seek; FX_INTERFACE_ENTRY(s_seek) = stream_seek;
FX_INTERFACE_ENTRY(s_tell) = stream_tell; FX_INTERFACE_ENTRY(s_tell) = stream_tell;
FX_TYPE_CLASS_INTERFACE_END(fx_stream, FX_TYPE_STREAM) FX_TYPE_VTABLE_INTERFACE_END(fx_stream, FX_TYPE_STREAM)
FX_TYPE_CLASS_DEFINITION_END(fx_file) FX_TYPE_CLASS_END(fx_file)
FX_TYPE_DEFINITION_BEGIN(fx_file) FX_TYPE_DEFINITION_BEGIN(fx_file)
FX_TYPE_ID(0x495a73f6, 0xb8c3, 0x4e17, 0xb5f4, 0x6fc321f67c7b); FX_TYPE_ID(0x495a73f6, 0xb8c3, 0x4e17, 0xb5f4, 0x6fc321f67c7b);
@@ -1,6 +1,6 @@
#include "misc.h" #include "misc.h"
#include <fx/core/random.h> #include <fx/random.h>
void z__fx_io_generate_tmp_filename(char *out, size_t len) void z__fx_io_generate_tmp_filename(char *out, size_t len)
{ {
@@ -1,11 +1,12 @@
#include "posix.h" #include "posix.h"
#include <fx/ds/string.h>
#include <fx/io/file.h>
#include <fx/io/path.h>
#include <ctype.h> #include <ctype.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <fx/io/file.h>
#include <fx/io/path.h>
#include <fx/string.h>
#include <fx/value.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/stat.h> #include <sys/stat.h>
@@ -45,7 +46,9 @@ static const char *path_ptr(const struct fx_path_p *path)
return fx_string_get_cstr(path->p_pathstr); return fx_string_get_cstr(path->p_pathstr);
} }
static enum fx_status path_stat(const struct fx_path_p *path, struct fx_file_info *out) static enum fx_status path_stat(
const struct fx_path_p *path,
struct fx_file_info *out)
{ {
struct stat st; struct stat st;
int err = stat(path_ptr(path), &st); int err = stat(path_ptr(path), &st);
@@ -110,11 +113,13 @@ static void append_path(struct fx_path_p *dest, const struct fx_path_p *src)
static enum fx_status path_unlink(const struct fx_path_p *path) static enum fx_status path_unlink(const struct fx_path_p *path)
{ {
int err = remove(fx_string_get_cstr(path->p_pathstr)); int err = remove(fx_string_get_cstr(path->p_pathstr));
return err == 0 ? FX_SUCCESS : fx_status_from_errno(errno, FX_ERR_IO_FAILURE); return err == 0 ? FX_SUCCESS
: fx_status_from_errno(errno, FX_ERR_IO_FAILURE);
} }
static enum fx_status path_get_directory( static enum fx_status path_get_directory(
const struct fx_path_p *path, fx_path **out_dir_path) const struct fx_path_p *path,
fx_path **out_dir_path)
{ {
fx_string *path_str = path->p_pathstr; fx_string *path_str = path->p_pathstr;
long len = fx_string_get_size(path_str, FX_STRLEN_NORMAL); long len = fx_string_get_size(path_str, FX_STRLEN_NORMAL);
@@ -130,7 +135,8 @@ static enum fx_status path_get_directory(
size_t dir_path_len = (size_t)(sep - path_cstr); size_t dir_path_len = (size_t)(sep - path_cstr);
fx_string *dir_path_s = fx_string_get_substr(path_str, 0, dir_path_len); fx_string *dir_path_s = fx_string_get_substr(path_str, 0, dir_path_len);
fx_path *dir_path = fx_path_create_from_cstr(fx_string_get_cstr(dir_path_s)); fx_path *dir_path = fx_path_create_from_cstr(
fx_string_get_cstr(dir_path_s));
fx_string_unref(dir_path_s); fx_string_unref(dir_path_s);
*out_dir_path = dir_path; *out_dir_path = dir_path;
@@ -139,7 +145,8 @@ static enum fx_status path_get_directory(
} }
static enum fx_status path_get_filename( static enum fx_status path_get_filename(
const struct fx_path_p *path, fx_string *out_name) const struct fx_path_p *path,
fx_string *out_name)
{ {
fx_string *path_str = path->p_pathstr; fx_string *path_str = path->p_pathstr;
long len = fx_string_get_size(path_str, FX_STRLEN_NORMAL); long len = fx_string_get_size(path_str, FX_STRLEN_NORMAL);
@@ -278,12 +285,15 @@ fx_path *fx_path_join(const fx_path *paths[], size_t nr_paths)
return NULL; return NULL;
} }
struct fx_path_p *result_p = fx_object_get_private(result, FX_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++) { for (size_t i = 0; i < nr_paths; i++) {
if (paths[i]) { if (paths[i]) {
struct fx_path_p *path_p struct fx_path_p *path_p = fx_object_get_private(
= fx_object_get_private(paths[i], FX_TYPE_PATH); paths[i],
FX_TYPE_PATH);
append_path(result_p, path_p); append_path(result_p, path_p);
} }
} }
@@ -336,14 +346,24 @@ enum fx_status fx_path_unlink(const fx_path *path)
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_PATH, path_unlink, path); FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_PATH, path_unlink, path);
} }
enum fx_status fx_path_get_directory(const fx_path *path, fx_path **out_dir_path) enum fx_status fx_path_get_directory(
const fx_path *path,
fx_path **out_dir_path)
{ {
FX_CLASS_DISPATCH_STATIC(FX_TYPE_PATH, path_get_directory, path, out_dir_path); FX_CLASS_DISPATCH_STATIC(
FX_TYPE_PATH,
path_get_directory,
path,
out_dir_path);
} }
enum fx_status fx_path_get_filename(const fx_path *path, fx_string *out_name) enum fx_status fx_path_get_filename(const fx_path *path, fx_string *out_name)
{ {
FX_CLASS_DISPATCH_STATIC(FX_TYPE_PATH, path_get_filename, path, out_name); FX_CLASS_DISPATCH_STATIC(
FX_TYPE_PATH,
path_get_filename,
path,
out_name);
} }
const char *fx_path_ptr(const fx_path *path) const char *fx_path_ptr(const fx_path *path)
@@ -368,27 +388,33 @@ static void path_init(fx_object *obj, void *priv)
} }
} }
void path_fini(fx_object *obj, void *priv) static void path_fini(fx_object *obj, void *priv)
{ {
struct fx_path_p *path = priv; struct fx_path_p *path = priv;
fx_string_unref(path->p_pathstr); fx_string_unref(path->p_pathstr);
} }
void path_to_string(const fx_object *obj, fx_stream *out) static fx_status path_to_string(
const fx_value *obj,
fx_stream *out,
const char *format)
{ {
struct fx_path_p *path = fx_object_get_private(obj, FX_TYPE_PATH); struct fx_path_p *path = fx_object_get_private(
obj->v_object,
FX_TYPE_PATH);
fx_stream_write_cstr(out, fx_string_get_cstr(path->p_pathstr), NULL); fx_stream_write_cstr(out, fx_string_get_cstr(path->p_pathstr), NULL);
return FX_SUCCESS;
} }
/*** CLASS DEFINITION *********************************************************/ /*** CLASS DEFINITION *********************************************************/
FX_TYPE_CLASS_DEFINITION_BEGIN(fx_path) FX_TYPE_CLASS_BEGIN(fx_path)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = path_to_string; FX_INTERFACE_ENTRY(to_string) = path_to_string;
FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT) FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_DEFINITION_END(fx_path) FX_TYPE_CLASS_END(fx_path)
FX_TYPE_DEFINITION_BEGIN(fx_path) FX_TYPE_DEFINITION_BEGIN(fx_path)
FX_TYPE_ID(0x56dc32eb, 0xea96, 0x46ed, 0x85d3, 0x760fa4ad61f4); FX_TYPE_ID(0x56dc32eb, 0xea96, 0x46ed, 0x85d3, 0x760fa4ad61f4);
@@ -1,7 +1,7 @@
#include <fx/core/error.h>
#include <fx/core/status.h>
#include <fx/io/file.h>
#include <errno.h> #include <errno.h>
#include <fx/error.h>
#include <fx/io/file.h>
#include <fx/status.h>
#include <sys/stat.h> #include <sys/stat.h>
enum fx_status fx_status_from_errno(int error, enum fx_status default_value) enum fx_status fx_status_from_errno(int error, enum fx_status default_value)
@@ -35,40 +35,53 @@ enum fx_status fx_status_from_errno(int error, enum fx_status default_value)
} }
fx_result fx_result_from_errno_with_filepath( fx_result fx_result_from_errno_with_filepath(
int error, const char *path, enum fx_status default_value) int error,
const char *path,
enum fx_status default_value)
{ {
switch (error) { switch (error) {
case 0: case 0:
return FX_RESULT_SUCCESS; return FX_RESULT_SUCCESS;
case ENOENT: case ENOENT:
return FX_RESULT_STATUS_WITH_STRING( return FX_RESULT_STATUS_WITH_STRING(
FX_ERR_NO_ENTRY, "Path @i{%s} does not exist", path); FX_ERR_NO_ENTRY,
"Path @i{%s} does not exist",
path);
case ENOTDIR: case ENOTDIR:
return FX_RESULT_STATUS_WITH_STRING( return FX_RESULT_STATUS_WITH_STRING(
FX_ERR_NOT_DIRECTORY, "Path @i{%s} is not a directory", FX_ERR_NOT_DIRECTORY,
"Path @i{%s} is not a directory",
path); path);
case EISDIR: case EISDIR:
return FX_RESULT_STATUS_WITH_STRING( return FX_RESULT_STATUS_WITH_STRING(
FX_ERR_IS_DIRECTORY, "Path @i{%s} is a directory", path); FX_ERR_IS_DIRECTORY,
"Path @i{%s} is a directory",
path);
case EPERM: case EPERM:
case EACCES: case EACCES:
return FX_RESULT_STATUS_WITH_STRING( return FX_RESULT_STATUS_WITH_STRING(
FX_ERR_PERMISSION_DENIED, FX_ERR_PERMISSION_DENIED,
"Permission denied while accessing path @i{%s}", path); "Permission denied while accessing path @i{%s}",
path);
default: default:
return FX_RESULT_STATUS(fx_status_from_errno(error, default_value)); return FX_RESULT_STATUS(
fx_status_from_errno(error, default_value));
} }
return FX_RESULT_SUCCESS; return FX_RESULT_SUCCESS;
} }
fx_result fx_result_from_errno_with_subfilepath( fx_result fx_result_from_errno_with_subfilepath(
int error, const char *path, const char *dir_path, int error,
const char *path,
const char *dir_path,
enum fx_status default_value) enum fx_status default_value)
{ {
if (!dir_path) { if (!dir_path) {
return fx_result_propagate(fx_result_from_errno_with_filepath( return fx_result_propagate(fx_result_from_errno_with_filepath(
error, path, default_value)); error,
path,
default_value));
} }
switch (error) { switch (error) {
@@ -77,17 +90,20 @@ fx_result fx_result_from_errno_with_subfilepath(
case ENOENT: case ENOENT:
return FX_RESULT_STATUS_WITH_STRING( return FX_RESULT_STATUS_WITH_STRING(
FX_ERR_NO_ENTRY, FX_ERR_NO_ENTRY,
"Path @i{%s} in directory @i{%s} does not exist", path, "Path @i{%s} in directory @i{%s} does not exist",
path,
dir_path); dir_path);
case ENOTDIR: case ENOTDIR:
return FX_RESULT_STATUS_WITH_STRING( return FX_RESULT_STATUS_WITH_STRING(
FX_ERR_NOT_DIRECTORY, FX_ERR_NOT_DIRECTORY,
"Path @i{%s} in directory @i{%s} is not a directory", "Path @i{%s} in directory @i{%s} is not a directory",
path, dir_path); path,
dir_path);
case EISDIR: case EISDIR:
return FX_RESULT_STATUS_WITH_STRING( return FX_RESULT_STATUS_WITH_STRING(
FX_ERR_IS_DIRECTORY, FX_ERR_IS_DIRECTORY,
"Path @i{%s} in directory @i{%s} is a directory", path, "Path @i{%s} in directory @i{%s} is a directory",
path,
dir_path); dir_path);
case EPERM: case EPERM:
case EACCES: case EACCES:
@@ -95,15 +111,19 @@ fx_result fx_result_from_errno_with_subfilepath(
FX_ERR_PERMISSION_DENIED, FX_ERR_PERMISSION_DENIED,
"Permission denied while accessing path @i{%s} in " "Permission denied while accessing path @i{%s} in "
"directory @i{%s}", "directory @i{%s}",
path, dir_path); path,
dir_path);
default: default:
return FX_RESULT_STATUS(fx_status_from_errno(error, default_value)); return FX_RESULT_STATUS(
fx_status_from_errno(error, default_value));
} }
return FX_RESULT_SUCCESS; return FX_RESULT_SUCCESS;
} }
enum fx_status fx_file_info_from_stat(const struct stat *st, struct fx_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; out->length = st->st_size;
+26
View File
@@ -0,0 +1,26 @@
#ifndef _IO_DARWIN_POSIX_H_
#define _IO_DARWIN_POSIX_H_
#include <fx/error.h>
#include <fx/status.h>
struct stat;
struct fx_file_info;
extern enum fx_status fx_status_from_errno(
int error,
enum fx_status default_value);
extern fx_result fx_result_from_errno_with_filepath(
int error,
const char *path,
enum fx_status default_value);
extern fx_result fx_result_from_errno_with_subfilepath(
int error,
const char *path,
const char *dir_path,
enum fx_status default_value);
extern enum fx_status fx_file_info_from_stat(
const struct stat *in,
struct fx_file_info *out);
#endif
-19
View File
@@ -1,19 +0,0 @@
#ifndef _IO_DARWIN_POSIX_H_
#define _IO_DARWIN_POSIX_H_
#include <fx/core/error.h>
#include <fx/core/status.h>
struct stat;
struct fx_file_info;
extern enum fx_status fx_status_from_errno(int error, enum fx_status default_value);
extern fx_result fx_result_from_errno_with_filepath(
int error, const char *path, enum fx_status default_value);
extern fx_result fx_result_from_errno_with_subfilepath(
int error, const char *path, const char *dir_path,
enum fx_status default_value);
extern enum fx_status fx_file_info_from_stat(
const struct stat *in, struct fx_file_info *out);
#endif
+132 -80
View File
@@ -6,9 +6,9 @@
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <fts.h> #include <fts.h>
#include <fx/core/error.h> #include <fx/error.h>
#include <fx/ds/string.h>
#include <fx/io/directory.h> #include <fx/io/directory.h>
#include <fx/string.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/stat.h> #include <sys/stat.h>
@@ -37,6 +37,7 @@ struct fx_directory_iterator_p {
fx_directory *root; fx_directory *root;
fx_directory_entry entry; fx_directory_entry entry;
fx_value entry_value;
}; };
/*** PRIVATE FUNCTIONS ********************************************************/ /*** PRIVATE FUNCTIONS ********************************************************/
@@ -60,7 +61,9 @@ static const char *directory_get_rel_path_cstr(const struct fx_directory_p *dir)
return fx_path_ptr(dir->d_path_rel); return fx_path_ptr(dir->d_path_rel);
} }
static fx_result directory_delete(fx_directory *dir, struct fx_directory_p *dir_p) static fx_result directory_delete(
fx_directory *dir,
struct fx_directory_p *dir_p)
{ {
enum fx_status status = FX_SUCCESS; enum fx_status status = FX_SUCCESS;
@@ -84,7 +87,8 @@ static fx_result directory_delete(fx_directory *dir, struct fx_directory_p *dir_
} }
static bool directory_path_exists( static bool directory_path_exists(
const struct fx_directory_p *root, const fx_path *path) const struct fx_directory_p *root,
const fx_path *path)
{ {
const fx_path *parts[] = { const fx_path *parts[] = {
root ? root->d_path_abs : NULL, root ? root->d_path_abs : NULL,
@@ -103,7 +107,8 @@ static bool directory_path_exists(
} }
static bool directory_path_is_file( static bool directory_path_is_file(
const struct fx_directory_p *root, const fx_path *path) const struct fx_directory_p *root,
const fx_path *path)
{ {
const fx_path *parts[] = { const fx_path *parts[] = {
root ? root->d_path_abs : NULL, root ? root->d_path_abs : NULL,
@@ -122,7 +127,8 @@ static bool directory_path_is_file(
} }
static bool directory_path_is_directory( static bool directory_path_is_directory(
const struct fx_directory_p *root, const fx_path *path) const struct fx_directory_p *root,
const fx_path *path)
{ {
const fx_path *parts[] = { const fx_path *parts[] = {
root ? root->d_path_abs : NULL, root ? root->d_path_abs : NULL,
@@ -141,7 +147,8 @@ static bool directory_path_is_directory(
} }
static fx_result directory_path_stat( static fx_result directory_path_stat(
const struct fx_directory_p *root, const fx_path *path, const struct fx_directory_p *root,
const fx_path *path,
struct fx_file_info *out) struct fx_file_info *out)
{ {
const fx_path *parts[] = { const fx_path *parts[] = {
@@ -161,7 +168,8 @@ static fx_result directory_path_stat(
} }
static fx_result directory_path_unlink( static fx_result directory_path_unlink(
const struct fx_directory_p *root, const fx_path *path) const struct fx_directory_p *root,
const fx_path *path)
{ {
const fx_path *parts[] = { const fx_path *parts[] = {
root ? root->d_path_abs : NULL, root ? root->d_path_abs : NULL,
@@ -195,11 +203,15 @@ static fx_result create_directory(struct fx_directory_p *root, const char *path)
} }
return fx_result_from_errno_with_subfilepath( return fx_result_from_errno_with_subfilepath(
errno, path, directory_get_rel_path_cstr(root), FX_ERR_IO_FAILURE); errno,
path,
directory_get_rel_path_cstr(root),
FX_ERR_IO_FAILURE);
} }
static fx_result create_directory_hierarchy( static fx_result create_directory_hierarchy(
struct fx_directory_p *root, const char *path) struct fx_directory_p *root,
const char *path)
{ {
int root_fd = root ? root->d_fd : AT_FDCWD; int root_fd = root ? root->d_fd : AT_FDCWD;
@@ -219,7 +231,9 @@ static fx_result create_directory_hierarchy(
int err = mkdirat(root_fd, path_buf, 0755); int err = mkdirat(root_fd, path_buf, 0755);
if (err != 0 && errno != EEXIST) { if (err != 0 && errno != EEXIST) {
result = fx_result_from_errno_with_subfilepath( result = fx_result_from_errno_with_subfilepath(
errno, path_buf, directory_get_rel_path_cstr(root), errno,
path_buf,
directory_get_rel_path_cstr(root),
FX_ERR_IO_FAILURE); FX_ERR_IO_FAILURE);
break; break;
} }
@@ -230,7 +244,9 @@ static fx_result create_directory_hierarchy(
int err = mkdirat(root_fd, path_buf, 0755); int err = mkdirat(root_fd, path_buf, 0755);
if (err != 0 && errno != EEXIST) { if (err != 0 && errno != EEXIST) {
result = fx_result_from_errno_with_subfilepath( result = fx_result_from_errno_with_subfilepath(
errno, path_buf, directory_get_rel_path_cstr(root), errno,
path_buf,
directory_get_rel_path_cstr(root),
FX_ERR_IO_FAILURE); FX_ERR_IO_FAILURE);
} }
@@ -239,8 +255,10 @@ static fx_result create_directory_hierarchy(
} }
static fx_result directory_open( static fx_result directory_open(
struct fx_directory_p *root, const fx_path *path, struct fx_directory_p *root,
fx_directory_open_flags flags, fx_directory **out) const fx_path *path,
fx_directory_open_flags flags,
fx_directory **out)
{ {
enum fx_status status = FX_SUCCESS; enum fx_status status = FX_SUCCESS;
@@ -257,7 +275,9 @@ static fx_result directory_open(
if ((flags & FX_DIRECTORY_OPEN_CREATE_INTERMEDIATE) if ((flags & FX_DIRECTORY_OPEN_CREATE_INTERMEDIATE)
== FX_DIRECTORY_OPEN_CREATE_INTERMEDIATE) { == FX_DIRECTORY_OPEN_CREATE_INTERMEDIATE) {
result = create_directory_hierarchy(root, path_cstr); result = create_directory_hierarchy(root, path_cstr);
} else if ((flags & FX_DIRECTORY_OPEN_CREATE) == FX_DIRECTORY_OPEN_CREATE) { } else if (
(flags & FX_DIRECTORY_OPEN_CREATE)
== FX_DIRECTORY_OPEN_CREATE) {
result = create_directory(root, path_cstr); result = create_directory(root, path_cstr);
} }
@@ -274,7 +294,8 @@ static fx_result directory_open(
fx_directory *dir = fx_object_create(FX_TYPE_DIRECTORY); fx_directory *dir = fx_object_create(FX_TYPE_DIRECTORY);
fx_path *cwd = NULL; fx_path *cwd = NULL;
struct fx_directory_p *p = fx_object_get_private(dir, FX_TYPE_DIRECTORY); struct fx_directory_p *p
= fx_object_get_private(dir, FX_TYPE_DIRECTORY);
if (!root) { if (!root) {
cwd = fx_path_create_cwd(); cwd = fx_path_create_cwd();
@@ -310,11 +331,18 @@ static fx_result directory_open(
/*** PUBLIC FUNCTIONS *********************************************************/ /*** PUBLIC FUNCTIONS *********************************************************/
fx_result fx_directory_open( fx_result fx_directory_open(
fx_directory *root, const fx_path *path, fx_directory_open_flags flags, fx_directory *root,
const fx_path *path,
fx_directory_open_flags flags,
fx_directory **out) fx_directory **out)
{ {
FX_CLASS_DISPATCH_STATIC( FX_CLASS_DISPATCH_STATIC(
FX_TYPE_DIRECTORY, directory_open, root, path, flags, out); FX_TYPE_DIRECTORY,
directory_open,
root,
path,
flags,
out);
} }
fx_result fx_directory_open_temp(fx_directory **out) fx_result fx_directory_open_temp(fx_directory **out)
@@ -329,7 +357,10 @@ fx_result fx_directory_open_temp(fx_directory **out)
fx_directory *dir = NULL; fx_directory *dir = NULL;
fx_result status = fx_directory_open( fx_result status = fx_directory_open(
FX_DIRECTORY_ROOT, rpath, FX_DIRECTORY_OPEN_CREATE, &dir); FX_DIRECTORY_ROOT,
rpath,
FX_DIRECTORY_OPEN_CREATE,
&dir);
struct fx_directory_p *p struct fx_directory_p *p
= fx_object_get_private(dir, FX_TYPE_DIRECTORY); = fx_object_get_private(dir, FX_TYPE_DIRECTORY);
@@ -356,23 +387,32 @@ const fx_path *fx_directory_get_path(const fx_directory *dir)
const fx_path *fx_directory_get_rel_path(const fx_directory *dir) const fx_path *fx_directory_get_rel_path(const fx_directory *dir)
{ {
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_DIRECTORY, directory_get_rel_path, dir); FX_CLASS_DISPATCH_STATIC_0(
FX_TYPE_DIRECTORY,
directory_get_rel_path,
dir);
} }
const char *fx_directory_get_path_cstr(const fx_directory *dir) const char *fx_directory_get_path_cstr(const fx_directory *dir)
{ {
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_DIRECTORY, directory_get_path_cstr, dir); FX_CLASS_DISPATCH_STATIC_0(
FX_TYPE_DIRECTORY,
directory_get_path_cstr,
dir);
} }
const char *fx_directory_get_rel_path_cstr(const fx_directory *dir) const char *fx_directory_get_rel_path_cstr(const fx_directory *dir)
{ {
FX_CLASS_DISPATCH_STATIC_0( FX_CLASS_DISPATCH_STATIC_0(
FX_TYPE_DIRECTORY, directory_get_rel_path_cstr, dir); FX_TYPE_DIRECTORY,
directory_get_rel_path_cstr,
dir);
} }
fx_result fx_directory_delete(fx_directory *dir) fx_result fx_directory_delete(fx_directory *dir)
{ {
struct fx_directory_p *p = fx_object_get_private(dir, FX_TYPE_DIRECTORY); struct fx_directory_p *p
= fx_object_get_private(dir, FX_TYPE_DIRECTORY);
p->d_flags |= DIRECTORY_DELETE_ON_CLOSE; p->d_flags |= DIRECTORY_DELETE_ON_CLOSE;
/* TODO allow object release functions to return a fx_result value */ /* TODO allow object release functions to return a fx_result value */
fx_directory_unref(dir); fx_directory_unref(dir);
@@ -382,32 +422,54 @@ fx_result fx_directory_delete(fx_directory *dir)
bool fx_directory_path_exists(const fx_directory *root, const fx_path *path) bool fx_directory_path_exists(const fx_directory *root, const fx_path *path)
{ {
FX_CLASS_DISPATCH_STATIC( FX_CLASS_DISPATCH_STATIC(
FX_TYPE_DIRECTORY, directory_path_exists, root, path); FX_TYPE_DIRECTORY,
directory_path_exists,
root,
path);
} }
bool fx_directory_path_is_file(const fx_directory *root, const fx_path *path) bool fx_directory_path_is_file(const fx_directory *root, const fx_path *path)
{ {
FX_CLASS_DISPATCH_STATIC( FX_CLASS_DISPATCH_STATIC(
FX_TYPE_DIRECTORY, directory_path_is_file, root, path); FX_TYPE_DIRECTORY,
directory_path_is_file,
root,
path);
} }
bool fx_directory_path_is_directory(const fx_directory *root, const fx_path *path) bool fx_directory_path_is_directory(
const fx_directory *root,
const fx_path *path)
{ {
FX_CLASS_DISPATCH_STATIC( FX_CLASS_DISPATCH_STATIC(
FX_TYPE_DIRECTORY, directory_path_is_directory, root, path); FX_TYPE_DIRECTORY,
directory_path_is_directory,
root,
path);
} }
fx_result fx_directory_path_stat( fx_result fx_directory_path_stat(
const fx_directory *root, const fx_path *path, struct fx_file_info *out) const fx_directory *root,
const fx_path *path,
struct fx_file_info *out)
{ {
FX_CLASS_DISPATCH_STATIC( FX_CLASS_DISPATCH_STATIC(
FX_TYPE_DIRECTORY, directory_path_stat, root, path, out); FX_TYPE_DIRECTORY,
directory_path_stat,
root,
path,
out);
} }
fx_result fx_directory_path_unlink(const fx_directory *root, const fx_path *path) fx_result fx_directory_path_unlink(
const fx_directory *root,
const fx_path *path)
{ {
FX_CLASS_DISPATCH_STATIC( FX_CLASS_DISPATCH_STATIC(
FX_TYPE_DIRECTORY, directory_path_unlink, root, path); FX_TYPE_DIRECTORY,
directory_path_unlink,
root,
path);
} }
/*** VIRTUAL FUNCTIONS ********************************************************/ /*** VIRTUAL FUNCTIONS ********************************************************/
@@ -449,6 +511,8 @@ static void update_iterator_data(struct fx_directory_iterator_p *it)
fx_path *path = fx_path_create_from_cstr( fx_path *path = fx_path_create_from_cstr(
ent->fts_path + fx_path_length(it->_p->d_path_abs) + 1); ent->fts_path + fx_path_length(it->_p->d_path_abs) + 1);
fx_value_unset(&it->entry_value);
it->entry_value = FX_POINTER(&it->entry);
it->entry.filename = ent->fts_name; it->entry.filename = ent->fts_name;
it->entry.filepath = path; it->entry.filepath = path;
@@ -492,7 +556,8 @@ static void iterator_fini(fx_object *obj, void *priv)
} }
fx_iterator *fx_directory_begin( fx_iterator *fx_directory_begin(
fx_directory *directory, enum fx_directory_iterator_flags flags) fx_directory *directory,
enum fx_directory_iterator_flags flags)
{ {
fx_iterator *it_obj = fx_object_create(FX_TYPE_DIRECTORY_ITERATOR); fx_iterator *it_obj = fx_object_create(FX_TYPE_DIRECTORY_ITERATOR);
struct fx_directory_iterator_p *it struct fx_directory_iterator_p *it
@@ -554,15 +619,11 @@ fx_iterator *fx_directory_begin(
return it_obj; return it_obj;
} }
static fx_iterator *iterator_begin(fx_object *obj) static const fx_iterator *iterator_begin(const fx_object *obj)
{
return fx_directory_begin(obj, FX_DIRECTORY_ITERATE_PARENT_FIRST);
}
static const fx_iterator *iterator_cbegin(const fx_object *obj)
{ {
return fx_directory_begin( return fx_directory_begin(
(fx_object *)obj, FX_DIRECTORY_ITERATE_PARENT_FIRST); (fx_object *)obj,
FX_DIRECTORY_ITERATE_PARENT_FIRST);
} }
static enum fx_status iterator_move_next(const fx_iterator *obj) static enum fx_status iterator_move_next(const fx_iterator *obj)
@@ -618,7 +679,8 @@ static enum fx_status iterator_erase(fx_iterator *obj)
{ {
struct fx_directory_iterator_p *it struct fx_directory_iterator_p *it
= fx_object_get_private(obj, FX_TYPE_DIRECTORY_ITERATOR); = fx_object_get_private(obj, FX_TYPE_DIRECTORY_ITERATOR);
fx_result result = fx_directory_path_unlink(it->root, it->entry.filepath); fx_result result
= fx_directory_path_unlink(it->root, it->entry.filepath);
if (fx_result_is_error(result)) { if (fx_result_is_error(result)) {
enum fx_status status = fx_error_get_status_code(result); enum fx_status status = fx_error_get_status_code(result);
fx_error_discard(result); fx_error_discard(result);
@@ -628,62 +690,52 @@ static enum fx_status iterator_erase(fx_iterator *obj)
return iterator_move_next(obj); return iterator_move_next(obj);
} }
static fx_iterator_value iterator_get_value(fx_iterator *obj) static const fx_value *iterator_get_value(const fx_iterator *obj)
{ {
struct fx_directory_iterator_p *it struct fx_directory_iterator_p *it
= fx_object_get_private(obj, FX_TYPE_DIRECTORY_ITERATOR); = fx_object_get_private(obj, FX_TYPE_DIRECTORY_ITERATOR);
return FX_ITERATOR_VALUE_PTR(&it->entry); return &it->entry_value;
}
static const fx_iterator_value iterator_get_cvalue(const fx_iterator *obj)
{
struct fx_directory_iterator_p *it
= fx_object_get_private(obj, FX_TYPE_DIRECTORY_ITERATOR);
return FX_ITERATOR_VALUE_CPTR(&it->entry);
} }
/*** CLASS DEFINITION *********************************************************/ /*** CLASS DEFINITION *********************************************************/
// ---- fx_directory DEFINITION // ---- fx_directory DEFINITION
FX_TYPE_CLASS_DEFINITION_BEGIN(fx_directory) FX_TYPE_CLASS_BEGIN(fx_directory)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL; FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT) FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_iterable, FX_TYPE_ITERABLE) FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterable, FX_TYPE_ITERABLE)
FX_INTERFACE_ENTRY(it_begin) = iterator_begin; FX_INTERFACE_ENTRY(it_begin) = iterator_begin;
FX_INTERFACE_ENTRY(it_cbegin) = iterator_cbegin; FX_TYPE_VTABLE_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE)
FX_TYPE_CLASS_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE) FX_TYPE_CLASS_END(fx_directory)
FX_TYPE_CLASS_DEFINITION_END(fx_directory)
FX_TYPE_DEFINITION_BEGIN(fx_directory) FX_TYPE_DEFINITION_BEGIN(fx_directory)
FX_TYPE_ID(0x10d36546, 0x7f96, 0x464b, 0xbc4d, 0xe504b283fa45); FX_TYPE_ID(0x10d36546, 0x7f96, 0x464b, 0xbc4d, 0xe504b283fa45);
FX_TYPE_CLASS(fx_directory_class); FX_TYPE_CLASS(fx_directory_class);
FX_TYPE_IMPLEMENTS(FX_TYPE_ITERABLE); FX_TYPE_IMPLEMENTS(FX_TYPE_ITERABLE);
FX_TYPE_INSTANCE_PRIVATE(struct fx_directory_p); FX_TYPE_INSTANCE_PRIVATE(struct fx_directory_p);
FX_TYPE_INSTANCE_INIT(directory_init); FX_TYPE_INSTANCE_INIT(directory_init);
FX_TYPE_INSTANCE_FINI(directory_fini); FX_TYPE_INSTANCE_FINI(directory_fini);
FX_TYPE_DEFINITION_END(fx_directory) FX_TYPE_DEFINITION_END(fx_directory)
// ---- fx_directory_iterator DEFINITION // ---- fx_directory_iterator DEFINITION
FX_TYPE_CLASS_DEFINITION_BEGIN(fx_directory_iterator) FX_TYPE_CLASS_BEGIN(fx_directory_iterator)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL; FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT) FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_iterator, FX_TYPE_ITERATOR) FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterator, FX_TYPE_ITERATOR)
FX_INTERFACE_ENTRY(it_move_next) = iterator_move_next; FX_INTERFACE_ENTRY(it_move_next) = iterator_move_next;
FX_INTERFACE_ENTRY(it_erase) = iterator_erase; FX_INTERFACE_ENTRY(it_erase) = iterator_erase;
FX_INTERFACE_ENTRY(it_get_value) = iterator_get_value; FX_INTERFACE_ENTRY(it_get_value) = iterator_get_value;
FX_INTERFACE_ENTRY(it_get_cvalue) = iterator_get_cvalue; FX_TYPE_VTABLE_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR)
FX_TYPE_CLASS_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR) FX_TYPE_CLASS_END(fx_directory_iterator)
FX_TYPE_CLASS_DEFINITION_END(fx_directory_iterator)
FX_TYPE_DEFINITION_BEGIN(fx_directory_iterator) FX_TYPE_DEFINITION_BEGIN(fx_directory_iterator)
FX_TYPE_ID(0xc707fce6, 0xc895, 0x4925, 0x8700, 0xa60641dee0cc); FX_TYPE_ID(0xc707fce6, 0xc895, 0x4925, 0x8700, 0xa60641dee0cc);
FX_TYPE_EXTENDS(FX_TYPE_ITERATOR); FX_TYPE_EXTENDS(FX_TYPE_ITERATOR);
FX_TYPE_CLASS(fx_directory_iterator_class); FX_TYPE_CLASS(fx_directory_iterator_class);
FX_TYPE_INSTANCE_PRIVATE(struct fx_directory_iterator_p); FX_TYPE_INSTANCE_PRIVATE(struct fx_directory_iterator_p);
FX_TYPE_DEFINITION_END(fx_directory_iterator) FX_TYPE_DEFINITION_END(fx_directory_iterator)
+106 -35
View File
@@ -3,13 +3,13 @@
#include "misc.h" #include "misc.h"
#include "posix.h" #include "posix.h"
#include <fx/core/random.h> #include <errno.h>
#include <fx/ds/string.h> #include <fcntl.h>
#include <fx/io/directory.h> #include <fx/io/directory.h>
#include <fx/io/file.h> #include <fx/io/file.h>
#include <fx/io/path.h> #include <fx/io/path.h>
#include <errno.h> #include <fx/random.h>
#include <fcntl.h> #include <fx/string.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@@ -22,7 +22,10 @@ static enum fx_status stream_close(fx_stream *);
static enum fx_status stream_getc(fx_stream *, int *); 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_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_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_seek(
fx_stream *,
long long,
fx_stream_seek_origin);
static enum fx_status stream_tell(const fx_stream *, size_t *); static enum fx_status stream_tell(const fx_stream *, size_t *);
/*** PRIVATE DATA *************************************************************/ /*** PRIVATE DATA *************************************************************/
@@ -65,7 +68,9 @@ static unsigned int fx_mode_to_unix_mode(enum fx_file_mode mode)
} }
static fx_result file_open_shadow( static fx_result file_open_shadow(
struct fx_file_p *original, enum fx_file_mode mode, fx_file **out) struct fx_file_p *original,
enum fx_file_mode mode,
fx_file **out)
{ {
mode |= FX_FILE_SHADOW | FX_FILE_DELETE_ON_CLOSE | FX_FILE_CREATE; mode |= FX_FILE_SHADOW | FX_FILE_DELETE_ON_CLOSE | FX_FILE_CREATE;
@@ -77,7 +82,8 @@ static fx_result file_open_shadow(
fx_string_prepend_cstr(filename, ".~"); fx_string_prepend_cstr(filename, ".~");
fx_path *shadow_filename = fx_path_create_from_cstr(fx_string_get_cstr(filename)); fx_path *shadow_filename
= fx_path_create_from_cstr(fx_string_get_cstr(filename));
fx_string_unref(filename); fx_string_unref(filename);
const fx_path *parts[] = { const fx_path *parts[] = {
@@ -96,7 +102,10 @@ static fx_result file_open_shadow(
fx_file *shadow_file; fx_file *shadow_file;
fx_result status = fx_file_open( fx_result status = fx_file_open(
FX_DIRECTORY_ROOT, shadow_filepath, mode, &shadow_file); FX_DIRECTORY_ROOT,
shadow_filepath,
mode,
&shadow_file);
fx_path_unref(shadow_filepath); fx_path_unref(shadow_filepath);
if (fx_result_is_error(status)) { if (fx_result_is_error(status)) {
@@ -112,7 +121,9 @@ static const fx_path *file_path(const struct fx_file_p *file)
return file->path; return file->path;
} }
static enum fx_status file_stat(struct fx_file_p *file, struct fx_file_info *out) static enum fx_status file_stat(
struct fx_file_p *file,
struct fx_file_info *out)
{ {
struct stat st; struct stat st;
int err = fstat(file->fd, &st); int err = fstat(file->fd, &st);
@@ -169,7 +180,9 @@ static enum fx_status file_resize(struct fx_file_p *file, size_t len)
} }
static enum fx_status file_seek( static enum fx_status file_seek(
struct fx_file_p *file, long long offset, enum fx_seek_basis basis) struct fx_file_p *file,
long long offset,
enum fx_seek_basis basis)
{ {
int whence; int whence;
switch (basis) { switch (basis) {
@@ -195,7 +208,8 @@ static enum fx_status file_seek(
} }
static enum fx_status file_swap_shadow( static enum fx_status file_swap_shadow(
struct fx_file_p *main_file, struct fx_file_p *shadow_file) struct fx_file_p *main_file,
struct fx_file_p *shadow_file)
{ {
if (main_file->mode & FX_FILE_SHADOW) { if (main_file->mode & FX_FILE_SHADOW) {
return FX_ERR_NOT_SUPPORTED; return FX_ERR_NOT_SUPPORTED;
@@ -237,7 +251,9 @@ static enum fx_status file_swap_shadow(
int err; int err;
err = rename(fx_path_ptr(main_file->path), fx_path_ptr(tmp_path)); err = rename(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(shadow_file->path),
fx_path_ptr(main_file->path));
err = rename(fx_path_ptr(tmp_path), fx_path_ptr(shadow_file->path)); err = rename(fx_path_ptr(tmp_path), fx_path_ptr(shadow_file->path));
fx_path_unref(tmp_path); fx_path_unref(tmp_path);
@@ -250,7 +266,11 @@ static enum fx_status file_swap_shadow(
} }
static enum fx_status file_read( static enum fx_status file_read(
struct fx_file_p *file, size_t offset, size_t len, void *buf, size_t *nr_read) struct fx_file_p *file,
size_t offset,
size_t len,
void *buf,
size_t *nr_read)
{ {
if (offset != FX_OFFSET_CURRENT) { if (offset != FX_OFFSET_CURRENT) {
lseek(file->fd, offset, SEEK_SET); lseek(file->fd, offset, SEEK_SET);
@@ -269,7 +289,10 @@ static enum fx_status file_read(
} }
static enum fx_status file_write( static enum fx_status file_write(
struct fx_file_p *file, size_t offset, size_t len, const void *buf, struct fx_file_p *file,
size_t offset,
size_t len,
const void *buf,
size_t *nr_written) size_t *nr_written)
{ {
if (offset != FX_OFFSET_CURRENT) { if (offset != FX_OFFSET_CURRENT) {
@@ -316,7 +339,10 @@ static enum fx_status stream_getc(fx_stream *stream, int *out)
} }
static enum fx_status stream_read( static enum fx_status stream_read(
fx_stream *stream, void *buf, size_t max, size_t *nr_read) fx_stream *stream,
void *buf,
size_t max,
size_t *nr_read)
{ {
struct fx_file_p *file = fx_object_get_private(stream, FX_TYPE_FILE); struct fx_file_p *file = fx_object_get_private(stream, FX_TYPE_FILE);
@@ -327,7 +353,10 @@ static enum fx_status stream_read(
} }
static enum fx_status stream_write( static enum fx_status stream_write(
fx_stream *stream, const void *buf, size_t count, size_t *nr_written) fx_stream *stream,
const void *buf,
size_t count,
size_t *nr_written)
{ {
struct fx_file_p *file = fx_object_get_private(stream, FX_TYPE_FILE); struct fx_file_p *file = fx_object_get_private(stream, FX_TYPE_FILE);
@@ -338,7 +367,9 @@ static enum fx_status stream_write(
} }
static enum fx_status stream_seek( static enum fx_status stream_seek(
fx_stream *stream, long long offset, fx_stream_seek_origin origin) fx_stream *stream,
long long offset,
fx_stream_seek_origin origin)
{ {
fx_seek_basis basis; fx_seek_basis basis;
switch (origin) { switch (origin) {
@@ -362,7 +393,8 @@ static enum fx_status stream_seek(
static enum fx_status stream_tell(const fx_stream *stream, size_t *pos) static enum fx_status stream_tell(const fx_stream *stream, size_t *pos)
{ {
const struct fx_file_p *file = fx_object_get_private(stream, FX_TYPE_FILE); const struct fx_file_p *file
= fx_object_get_private(stream, FX_TYPE_FILE);
off_t v = lseek(file->fd, 0, SEEK_CUR); off_t v = lseek(file->fd, 0, SEEK_CUR);
if (v == (off_t)-1) { if (v == (off_t)-1) {
return fx_status_from_errno(errno, FX_ERR_IO_FAILURE); return fx_status_from_errno(errno, FX_ERR_IO_FAILURE);
@@ -375,7 +407,10 @@ static enum fx_status stream_tell(const fx_stream *stream, size_t *pos)
/*** PUBLIC FUNCTIONS *********************************************************/ /*** PUBLIC FUNCTIONS *********************************************************/
fx_result fx_file_open( fx_result fx_file_open(
fx_directory *root, const fx_path *path, enum fx_file_mode mode, fx_file **out) fx_directory *root,
const fx_path *path,
enum fx_file_mode mode,
fx_file **out)
{ {
const fx_path *file_path = path; const fx_path *file_path = path;
unsigned int flags = fx_mode_to_unix_mode(mode); unsigned int flags = fx_mode_to_unix_mode(mode);
@@ -458,7 +493,10 @@ fx_result fx_file_open_temp(enum fx_file_mode mode, fx_file **out)
fx_path *rpath = fx_path_create_from_cstr(path); fx_path *rpath = fx_path_create_from_cstr(path);
fx_result status = fx_file_open( fx_result status = fx_file_open(
FX_DIRECTORY_ROOT, rpath, mode | FX_FILE_CREATE_ONLY, out); FX_DIRECTORY_ROOT,
rpath,
mode | FX_FILE_CREATE_ONLY,
out);
if (fx_error_get_status_code(status) == FX_ERR_NAME_EXISTS) { if (fx_error_get_status_code(status) == FX_ERR_NAME_EXISTS) {
fx_path_unref(rpath); fx_path_unref(rpath);
@@ -472,9 +510,17 @@ fx_result fx_file_open_temp(enum fx_file_mode mode, fx_file **out)
} }
} }
fx_result fx_file_open_shadow(fx_file *original, enum fx_file_mode mode, fx_file **out) fx_result fx_file_open_shadow(
fx_file *original,
enum fx_file_mode mode,
fx_file **out)
{ {
FX_CLASS_DISPATCH_STATIC(FX_TYPE_FILE, file_open_shadow, original, mode, out); FX_CLASS_DISPATCH_STATIC(
FX_TYPE_FILE,
file_open_shadow,
original,
mode,
out);
} }
const fx_path *fx_file_path(const fx_file *file) const fx_path *fx_file_path(const fx_file *file)
@@ -502,30 +548,55 @@ enum fx_status fx_file_resize(fx_file *file, size_t len)
FX_CLASS_DISPATCH_STATIC(FX_TYPE_FILE, file_resize, file, len); FX_CLASS_DISPATCH_STATIC(FX_TYPE_FILE, file_resize, file, len);
} }
enum fx_status fx_file_seek(fx_file *file, long long offset, enum fx_seek_basis basis) enum fx_status fx_file_seek(
fx_file *file,
long long offset,
enum fx_seek_basis basis)
{ {
FX_CLASS_DISPATCH_STATIC(FX_TYPE_FILE, file_seek, file, offset, basis); FX_CLASS_DISPATCH_STATIC(FX_TYPE_FILE, file_seek, file, offset, basis);
} }
enum fx_status fx_file_swap_shadow(fx_file *main_file, fx_file *shadow_file) enum fx_status fx_file_swap_shadow(fx_file *main_file, fx_file *shadow_file)
{ {
struct fx_file_p *main_p = fx_object_get_private(main_file, FX_TYPE_FILE); struct fx_file_p *main_p
struct fx_file_p *shadow_p = fx_object_get_private(main_file, FX_TYPE_FILE); = 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); return file_swap_shadow(main_p, shadow_p);
} }
enum fx_status fx_file_read( enum fx_status fx_file_read(
fx_file *file, size_t offset, size_t len, void *buf, size_t *nr_read) fx_file *file,
size_t offset,
size_t len,
void *buf,
size_t *nr_read)
{ {
FX_CLASS_DISPATCH_STATIC( FX_CLASS_DISPATCH_STATIC(
FX_TYPE_FILE, file_read, file, offset, len, buf, nr_read); FX_TYPE_FILE,
file_read,
file,
offset,
len,
buf,
nr_read);
} }
enum fx_status fx_file_write( enum fx_status fx_file_write(
fx_file *file, size_t offset, size_t len, const void *buf, size_t *nr_written) fx_file *file,
size_t offset,
size_t len,
const void *buf,
size_t *nr_written)
{ {
FX_CLASS_DISPATCH_STATIC( FX_CLASS_DISPATCH_STATIC(
FX_TYPE_FILE, file_write, file, offset, len, buf, nr_written); FX_TYPE_FILE,
file_write,
file,
offset,
len,
buf,
nr_written);
} }
/*** VIRTUAL FUNCTIONS ********************************************************/ /*** VIRTUAL FUNCTIONS ********************************************************/
@@ -549,20 +620,20 @@ static void file_fini(fx_object *obj, void *priv)
/*** CLASS DEFINITION *********************************************************/ /*** CLASS DEFINITION *********************************************************/
FX_TYPE_CLASS_DEFINITION_BEGIN(fx_file) FX_TYPE_CLASS_BEGIN(fx_file)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL; FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT) FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_stream, FX_TYPE_STREAM) FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_stream, FX_TYPE_STREAM)
FX_INTERFACE_ENTRY(s_close) = stream_close; FX_INTERFACE_ENTRY(s_close) = stream_close;
FX_INTERFACE_ENTRY(s_getc) = stream_getc; FX_INTERFACE_ENTRY(s_getc) = stream_getc;
FX_INTERFACE_ENTRY(s_read) = stream_read; FX_INTERFACE_ENTRY(s_read) = stream_read;
FX_INTERFACE_ENTRY(s_write) = stream_write; FX_INTERFACE_ENTRY(s_write) = stream_write;
FX_INTERFACE_ENTRY(s_seek) = stream_seek; FX_INTERFACE_ENTRY(s_seek) = stream_seek;
FX_INTERFACE_ENTRY(s_tell) = stream_tell; FX_INTERFACE_ENTRY(s_tell) = stream_tell;
FX_TYPE_CLASS_INTERFACE_END(fx_stream, FX_TYPE_STREAM) FX_TYPE_VTABLE_INTERFACE_END(fx_stream, FX_TYPE_STREAM)
FX_TYPE_CLASS_DEFINITION_END(fx_file) FX_TYPE_CLASS_END(fx_file)
FX_TYPE_DEFINITION_BEGIN(fx_file) FX_TYPE_DEFINITION_BEGIN(fx_file)
FX_TYPE_ID(0x495a73f6, 0xb8c3, 0x4e17, 0xb5f4, 0x6fc321f67c7b); FX_TYPE_ID(0x495a73f6, 0xb8c3, 0x4e17, 0xb5f4, 0x6fc321f67c7b);
+1 -1
View File
@@ -1,6 +1,6 @@
#include "misc.h" #include "misc.h"
#include <fx/core/random.h> #include <fx/random.h>
void z__fx_io_generate_tmp_filename(char *out, size_t len) void z__fx_io_generate_tmp_filename(char *out, size_t len)
{ {
+43 -19
View File
@@ -1,11 +1,11 @@
#include "posix.h" #include "posix.h"
#include <fx/ds/string.h>
#include <fx/io/file.h>
#include <fx/io/path.h>
#include <ctype.h> #include <ctype.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <fx/io/file.h>
#include <fx/io/path.h>
#include <fx/string.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/stat.h> #include <sys/stat.h>
@@ -45,7 +45,9 @@ static const char *path_ptr(const struct fx_path_p *path)
return fx_string_get_cstr(path->p_pathstr); return fx_string_get_cstr(path->p_pathstr);
} }
static enum fx_status path_stat(const struct fx_path_p *path, struct fx_file_info *out) static enum fx_status path_stat(
const struct fx_path_p *path,
struct fx_file_info *out)
{ {
struct stat st; struct stat st;
int err = stat(path_ptr(path), &st); int err = stat(path_ptr(path), &st);
@@ -110,11 +112,13 @@ static void append_path(struct fx_path_p *dest, const struct fx_path_p *src)
static enum fx_status path_unlink(const struct fx_path_p *path) static enum fx_status path_unlink(const struct fx_path_p *path)
{ {
int err = remove(fx_string_get_cstr(path->p_pathstr)); int err = remove(fx_string_get_cstr(path->p_pathstr));
return err == 0 ? FX_SUCCESS : fx_status_from_errno(errno, FX_ERR_IO_FAILURE); return err == 0 ? FX_SUCCESS
: fx_status_from_errno(errno, FX_ERR_IO_FAILURE);
} }
static enum fx_status path_get_directory( static enum fx_status path_get_directory(
const struct fx_path_p *path, fx_path **out_dir_path) const struct fx_path_p *path,
fx_path **out_dir_path)
{ {
fx_string *path_str = path->p_pathstr; fx_string *path_str = path->p_pathstr;
long len = fx_string_get_size(path_str, FX_STRLEN_NORMAL); long len = fx_string_get_size(path_str, FX_STRLEN_NORMAL);
@@ -130,7 +134,8 @@ static enum fx_status path_get_directory(
size_t dir_path_len = (size_t)(sep - path_cstr); size_t dir_path_len = (size_t)(sep - path_cstr);
fx_string *dir_path_s = fx_string_get_substr(path_str, 0, dir_path_len); fx_string *dir_path_s = fx_string_get_substr(path_str, 0, dir_path_len);
fx_path *dir_path = fx_path_create_from_cstr(fx_string_get_cstr(dir_path_s)); fx_path *dir_path
= fx_path_create_from_cstr(fx_string_get_cstr(dir_path_s));
fx_string_unref(dir_path_s); fx_string_unref(dir_path_s);
*out_dir_path = dir_path; *out_dir_path = dir_path;
@@ -139,7 +144,8 @@ static enum fx_status path_get_directory(
} }
static enum fx_status path_get_filename( static enum fx_status path_get_filename(
const struct fx_path_p *path, fx_string *out_name) const struct fx_path_p *path,
fx_string *out_name)
{ {
fx_string *path_str = path->p_pathstr; fx_string *path_str = path->p_pathstr;
long len = fx_string_get_size(path_str, FX_STRLEN_NORMAL); long len = fx_string_get_size(path_str, FX_STRLEN_NORMAL);
@@ -278,7 +284,8 @@ fx_path *fx_path_join(const fx_path *paths[], size_t nr_paths)
return NULL; return NULL;
} }
struct fx_path_p *result_p = fx_object_get_private(result, FX_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++) { for (size_t i = 0; i < nr_paths; i++) {
if (paths[i]) { if (paths[i]) {
@@ -336,14 +343,24 @@ enum fx_status fx_path_unlink(const fx_path *path)
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_PATH, path_unlink, path); FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_PATH, path_unlink, path);
} }
enum fx_status fx_path_get_directory(const fx_path *path, fx_path **out_dir_path) enum fx_status fx_path_get_directory(
const fx_path *path,
fx_path **out_dir_path)
{ {
FX_CLASS_DISPATCH_STATIC(FX_TYPE_PATH, path_get_directory, path, out_dir_path); FX_CLASS_DISPATCH_STATIC(
FX_TYPE_PATH,
path_get_directory,
path,
out_dir_path);
} }
enum fx_status fx_path_get_filename(const fx_path *path, fx_string *out_name) enum fx_status fx_path_get_filename(const fx_path *path, fx_string *out_name)
{ {
FX_CLASS_DISPATCH_STATIC(FX_TYPE_PATH, path_get_filename, path, out_name); FX_CLASS_DISPATCH_STATIC(
FX_TYPE_PATH,
path_get_filename,
path,
out_name);
} }
const char *fx_path_ptr(const fx_path *path) const char *fx_path_ptr(const fx_path *path)
@@ -375,20 +392,27 @@ void path_fini(fx_object *obj, void *priv)
fx_string_unref(path->p_pathstr); fx_string_unref(path->p_pathstr);
} }
void path_to_string(const fx_object *obj, fx_stream *out) static fx_status path_to_string(
const fx_value *obj,
fx_stream *out,
const char *format)
{ {
struct fx_path_p *path = fx_object_get_private(obj, FX_TYPE_PATH); struct fx_path_p *path
= fx_object_get_private(obj->v_object, FX_TYPE_PATH);
fx_stream_write_cstr(out, fx_string_get_cstr(path->p_pathstr), NULL); return fx_stream_write_cstr(
out,
fx_string_get_cstr(path->p_pathstr),
NULL);
} }
/*** CLASS DEFINITION *********************************************************/ /*** CLASS DEFINITION *********************************************************/
FX_TYPE_CLASS_DEFINITION_BEGIN(fx_path) FX_TYPE_CLASS_BEGIN(fx_path)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = path_to_string; FX_INTERFACE_ENTRY(to_string) = path_to_string;
FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT) FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_DEFINITION_END(fx_path) FX_TYPE_CLASS_END(fx_path)
FX_TYPE_DEFINITION_BEGIN(fx_path) FX_TYPE_DEFINITION_BEGIN(fx_path)
FX_TYPE_ID(0x56dc32eb, 0xea96, 0x46ed, 0x85d3, 0x760fa4ad61f4); FX_TYPE_ID(0x56dc32eb, 0xea96, 0x46ed, 0x85d3, 0x760fa4ad61f4);
+37 -17
View File
@@ -1,7 +1,7 @@
#include <fx/core/error.h>
#include <fx/core/status.h>
#include <fx/io/file.h>
#include <errno.h> #include <errno.h>
#include <fx/error.h>
#include <fx/io/file.h>
#include <fx/status.h>
#include <sys/stat.h> #include <sys/stat.h>
enum fx_status fx_status_from_errno(int error, enum fx_status default_value) enum fx_status fx_status_from_errno(int error, enum fx_status default_value)
@@ -35,40 +35,53 @@ enum fx_status fx_status_from_errno(int error, enum fx_status default_value)
} }
fx_result fx_result_from_errno_with_filepath( fx_result fx_result_from_errno_with_filepath(
int error, const char *path, enum fx_status default_value) int error,
const char *path,
enum fx_status default_value)
{ {
switch (error) { switch (error) {
case 0: case 0:
return FX_RESULT_SUCCESS; return FX_RESULT_SUCCESS;
case ENOENT: case ENOENT:
return FX_RESULT_STATUS_WITH_STRING( return FX_RESULT_STATUS_WITH_STRING(
FX_ERR_NO_ENTRY, "Path @i{%s} does not exist", path); FX_ERR_NO_ENTRY,
"Path @i{%s} does not exist",
path);
case ENOTDIR: case ENOTDIR:
return FX_RESULT_STATUS_WITH_STRING( return FX_RESULT_STATUS_WITH_STRING(
FX_ERR_NOT_DIRECTORY, "Path @i{%s} is not a directory", FX_ERR_NOT_DIRECTORY,
"Path @i{%s} is not a directory",
path); path);
case EISDIR: case EISDIR:
return FX_RESULT_STATUS_WITH_STRING( return FX_RESULT_STATUS_WITH_STRING(
FX_ERR_IS_DIRECTORY, "Path @i{%s} is a directory", path); FX_ERR_IS_DIRECTORY,
"Path @i{%s} is a directory",
path);
case EPERM: case EPERM:
case EACCES: case EACCES:
return FX_RESULT_STATUS_WITH_STRING( return FX_RESULT_STATUS_WITH_STRING(
FX_ERR_PERMISSION_DENIED, FX_ERR_PERMISSION_DENIED,
"Permission denied while accessing path @i{%s}", path); "Permission denied while accessing path @i{%s}",
path);
default: default:
return FX_RESULT_STATUS(fx_status_from_errno(error, default_value)); return FX_RESULT_STATUS(
fx_status_from_errno(error, default_value));
} }
return FX_RESULT_SUCCESS; return FX_RESULT_SUCCESS;
} }
fx_result fx_result_from_errno_with_subfilepath( fx_result fx_result_from_errno_with_subfilepath(
int error, const char *path, const char *dir_path, int error,
const char *path,
const char *dir_path,
enum fx_status default_value) enum fx_status default_value)
{ {
if (!dir_path) { if (!dir_path) {
return fx_result_propagate(fx_result_from_errno_with_filepath( return fx_result_propagate(fx_result_from_errno_with_filepath(
error, path, default_value)); error,
path,
default_value));
} }
switch (error) { switch (error) {
@@ -77,17 +90,20 @@ fx_result fx_result_from_errno_with_subfilepath(
case ENOENT: case ENOENT:
return FX_RESULT_STATUS_WITH_STRING( return FX_RESULT_STATUS_WITH_STRING(
FX_ERR_NO_ENTRY, FX_ERR_NO_ENTRY,
"Path @i{%s} in directory @i{%s} does not exist", path, "Path @i{%s} in directory @i{%s} does not exist",
path,
dir_path); dir_path);
case ENOTDIR: case ENOTDIR:
return FX_RESULT_STATUS_WITH_STRING( return FX_RESULT_STATUS_WITH_STRING(
FX_ERR_NOT_DIRECTORY, FX_ERR_NOT_DIRECTORY,
"Path @i{%s} in directory @i{%s} is not a directory", "Path @i{%s} in directory @i{%s} is not a directory",
path, dir_path); path,
dir_path);
case EISDIR: case EISDIR:
return FX_RESULT_STATUS_WITH_STRING( return FX_RESULT_STATUS_WITH_STRING(
FX_ERR_IS_DIRECTORY, FX_ERR_IS_DIRECTORY,
"Path @i{%s} in directory @i{%s} is a directory", path, "Path @i{%s} in directory @i{%s} is a directory",
path,
dir_path); dir_path);
case EPERM: case EPERM:
case EACCES: case EACCES:
@@ -95,15 +111,19 @@ fx_result fx_result_from_errno_with_subfilepath(
FX_ERR_PERMISSION_DENIED, FX_ERR_PERMISSION_DENIED,
"Permission denied while accessing path @i{%s} in " "Permission denied while accessing path @i{%s} in "
"directory @i{%s}", "directory @i{%s}",
path, dir_path); path,
dir_path);
default: default:
return FX_RESULT_STATUS(fx_status_from_errno(error, default_value)); return FX_RESULT_STATUS(
fx_status_from_errno(error, default_value));
} }
return FX_RESULT_SUCCESS; return FX_RESULT_SUCCESS;
} }
enum fx_status fx_file_info_from_stat(const struct stat *st, struct fx_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; out->length = st->st_size;
+13 -6
View File
@@ -1,19 +1,26 @@
#ifndef _IO_DARWIN_POSIX_H_ #ifndef _IO_DARWIN_POSIX_H_
#define _IO_DARWIN_POSIX_H_ #define _IO_DARWIN_POSIX_H_
#include <fx/core/error.h> #include <fx/error.h>
#include <fx/core/status.h> #include <fx/status.h>
struct stat; struct stat;
struct fx_file_info; struct fx_file_info;
extern enum fx_status fx_status_from_errno(int error, enum fx_status default_value); extern enum fx_status fx_status_from_errno(
int error,
enum fx_status default_value);
extern fx_result fx_result_from_errno_with_filepath( extern fx_result fx_result_from_errno_with_filepath(
int error, const char *path, enum fx_status default_value); int error,
const char *path,
enum fx_status default_value);
extern fx_result fx_result_from_errno_with_subfilepath( extern fx_result fx_result_from_errno_with_subfilepath(
int error, const char *path, const char *dir_path, int error,
const char *path,
const char *dir_path,
enum fx_status default_value); enum fx_status default_value);
extern enum fx_status fx_file_info_from_stat( extern enum fx_status fx_file_info_from_stat(
const struct stat *in, struct fx_file_info *out); const struct stat *in,
struct fx_file_info *out);
#endif #endif
+5 -2
View File
@@ -1,6 +1,6 @@
#include <fx/core/stream.h>
#include <fx/io/file.h> #include <fx/io/file.h>
#include <fx/io/path.h> #include <fx/io/path.h>
#include <fx/stream.h>
#include <stdio.h> #include <stdio.h>
int main(int argc, const char **argv) int main(int argc, const char **argv)
@@ -8,7 +8,10 @@ int main(int argc, const char **argv)
fx_file *dest; fx_file *dest;
fx_path *path = fx_path_create_from_cstr("data.txt"); fx_path *path = fx_path_create_from_cstr("data.txt");
fx_result result = fx_file_open( fx_result result = fx_file_open(
NULL, path, FX_FILE_WRITE_ONLY | FX_FILE_CREATE, &dest); NULL,
path,
FX_FILE_WRITE_ONLY | FX_FILE_CREATE,
&dest);
if (fx_result_is_error(result)) { if (fx_result_is_error(result)) {
fx_throw(result); fx_throw(result);
return -1; return -1;
+6 -2
View File
@@ -20,9 +20,13 @@ int main(int argc, const char **argv)
return -1; return -1;
} }
fx_iterator *it = fx_directory_begin(dir, FX_DIRECTORY_ITERATE_PARENT_FIRST); fx_iterator *it = fx_directory_begin(
fx_foreach(fx_directory_entry *, entry, it) dir,
FX_DIRECTORY_ITERATE_PARENT_FIRST);
fx_foreach(val, it)
{ {
fx_directory_entry *entry = NULL;
fx_value_get_pointer(val, (void **)&entry);
printf("%s\n", fx_path_ptr(entry->filepath)); printf("%s\n", fx_path_ptr(entry->filepath));
} }
+1 -6
View File
@@ -1,6 +1 @@
set(namespace_include_paths ${CMAKE_CURRENT_SOURCE_DIR}/include PARENT_SCOPE) export_fx_namespace_details(fx.reflection)
file(GLOB namespace_sources
*.c *.h
${CMAKE_CURRENT_SOURCE_DIR}/include/fx/reflection/*.h)
set(namespace_sources ${namespace_sources} PARENT_SCOPE)
+370 -32
View File
@@ -1,8 +1,24 @@
#include "fx/namemap.h"
#include <fx/bst.h> #include <fx/bst.h>
#include <fx/hash.h> #include <fx/hash.h>
#include <fx/iterator.h>
#include <fx/macros.h> #include <fx/macros.h>
#include <fx/queue.h> #include <fx/queue.h>
#include <fx/reflection/assembly.h> #include <fx/reflection/assembly.h>
#include <fx/reflection/type.h>
FX_API fx_type_id fx_assembly_iterator_get_type();
FX_API fx_type_id fx_assembly_type_iterator_get_type();
FX_DECLARE_TYPE(fx_assembly_iterator);
FX_DECLARE_TYPE(fx_assembly_type_iterator);
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_assembly_iterator)
FX_TYPE_CLASS_DECLARATION_END(fx_assembly_iterator)
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_assembly_type_iterator)
FX_TYPE_CLASS_DECLARATION_END(fx_assembly_type_iterator)
enum map_entry_type { enum map_entry_type {
MAP_ENTRY_NONE = 0, MAP_ENTRY_NONE = 0,
@@ -12,6 +28,7 @@ enum map_entry_type {
struct map_entry { struct map_entry {
enum map_entry_type e_type; enum map_entry_type e_type;
struct map_bucket *e_bucket;
uint64_t e_hash; uint64_t e_hash;
union { union {
fx_queue_entry e_entry; fx_queue_entry e_entry;
@@ -35,12 +52,14 @@ struct map {
struct type { struct type {
struct map_item e_map_item; struct map_item e_map_item;
fx_type e_type; fx_type_id e_type;
}; };
struct fx_assembly_p { struct fx_assembly_p {
fx_assembly *a_self;
const char *a_name; const char *a_name;
struct map a_types; struct map a_types;
fx_namemap_entry a_entry;
struct { struct {
long v_major; long v_major;
@@ -50,6 +69,16 @@ struct fx_assembly_p {
} a_version; } a_version;
}; };
struct fx_assembly_iterator_p {
fx_namemap_entry *it_cur;
fx_value it_value;
};
struct fx_assembly_type_iterator_p {
struct type *it_item;
fx_value it_value;
};
/*** PRIVATE FUNCTIONS ********************************************************/ /*** PRIVATE FUNCTIONS ********************************************************/
FX_BST_DEFINE_SIMPLE_GET( FX_BST_DEFINE_SIMPLE_GET(
@@ -60,6 +89,8 @@ FX_BST_DEFINE_SIMPLE_GET(
map_get_entry); map_get_entry);
FX_BST_DEFINE_SIMPLE_INSERT(struct map_entry, e_node, e_hash, map_put_entry); FX_BST_DEFINE_SIMPLE_INSERT(struct map_entry, e_node, e_hash, map_put_entry);
static fx_namemap assembly_map = FX_NAMEMAP_INIT;
static struct map_bucket *map_item_convert_to_bucket( static struct map_bucket *map_item_convert_to_bucket(
struct map *map, struct map *map,
struct map_item *item) struct map_item *item)
@@ -78,6 +109,28 @@ static struct map_bucket *map_item_convert_to_bucket(
return bucket; return bucket;
} }
static struct map_item *map_first_item(const struct map *map)
{
fx_bst_node *first = fx_bst_first(&map->m_entries);
if (!first) {
return NULL;
}
struct map_entry *entry = fx_unbox(struct map_entry, first, e_node);
if (entry->e_type == MAP_ENTRY_ITEM) {
return (struct map_item *)entry;
}
struct map_bucket *bucket = (struct map_bucket *)entry;
fx_queue_entry *first_entry = fx_queue_first(&bucket->b_items);
if (!first_entry) {
return NULL;
}
entry = fx_unbox(struct map_entry, first_entry, e_entry);
return (struct map_item *)entry;
}
static void map_put(struct map *map, const char *name, struct map_item *item) static void map_put(struct map *map, const char *name, struct map_item *item)
{ {
uint64_t hash = fx_hash_cstr(name); uint64_t hash = fx_hash_cstr(name);
@@ -109,34 +162,81 @@ static void map_put(struct map *map, const char *name, struct map_item *item)
} }
} }
static void assembly_set_name(struct fx_assembly_p *asm, const char *name) static struct map_item *map_item_next(struct map_item *item)
{ {
asm->a_name = name; struct map_bucket *bucket = item->i_entry.e_bucket;
struct map_entry *next_item = NULL;
if (bucket) {
struct fx_queue_entry *q_item
= fx_queue_next(&item->i_entry.e_entry);
if (!q_item) {
struct fx_bst_node *node
= fx_bst_next(&bucket->b_entry.e_node);
next_item = fx_unbox(struct map_entry, node, e_node);
} else {
next_item = fx_unbox(struct map_entry, q_item, e_entry);
}
} else {
struct fx_bst_node *node = fx_bst_next(&item->i_entry.e_node);
next_item = fx_unbox(struct map_entry, node, e_node);
}
if (!next_item) {
return NULL;
}
if (next_item->e_type == MAP_ENTRY_BUCKET) {
bucket = (struct map_bucket *)next_item;
struct fx_queue_entry *q_item
= fx_queue_first(&bucket->b_items);
next_item = fx_unbox(struct map_entry, q_item, e_entry);
}
return (struct map_item *)next_item;
}
static fx_status assembly_register(struct fx_assembly_p *assembly)
{
if (fx_namemap_get(&assembly_map, assembly->a_name)) {
return FX_ERR_NAME_EXISTS;
}
fx_namemap_put(&assembly_map, assembly->a_name, &assembly->a_entry);
return FX_SUCCESS;
}
static void assembly_set_name(struct fx_assembly_p *assembly, const char *name)
{
assembly->a_name = name;
} }
static void assembly_set_version( static void assembly_set_version(
struct fx_assembly_p *asm, struct fx_assembly_p *assembly,
long major, long major,
long minor, long minor,
long build, long build,
long revision) long revision)
{ {
asm->a_version.v_major = major; assembly->a_version.v_major = major;
asm->a_version.v_minor = minor; assembly->a_version.v_minor = minor;
asm->a_version.v_build = build; assembly->a_version.v_build = build;
asm->a_version.v_revision = revision; assembly->a_version.v_revision = revision;
} }
#include <stdio.h> #include <stdio.h>
static void assembly_add_type( static void assembly_add_type(
struct fx_assembly_p *asm, struct fx_assembly_p *assembly,
const char *full_name, const char *full_name,
fx_type type_id) fx_type_id type_id)
{ {
struct type *type = malloc(sizeof *type); struct type *type = malloc(sizeof *type);
memset(type, 0x0, sizeof *type); memset(type, 0x0, sizeof *type);
map_put(&asm->a_types, full_name, &type->e_map_item); type->e_type = type_id;
fx_type *ty = (fx_type *)fx_type_get_by_id(type_id);
fx_type_set_assembly(ty, assembly->a_self);
map_put(&assembly->a_types, full_name, &type->e_map_item);
} }
static void map_item_dump(struct map_item *item) static void map_item_dump(struct map_item *item)
@@ -145,17 +245,17 @@ static void map_item_dump(struct map_item *item)
printf(" Type: %s\n", type->e_map_item.i_name); printf(" Type: %s\n", type->e_map_item.i_name);
} }
static void assembly_dump(struct fx_assembly_p *asm) static void assembly_dump(struct fx_assembly_p *assembly)
{ {
printf("Loaded assembly:\n"); printf("Loaded assembly:\n");
printf(" %s, Version=%ld.%ld.%ld.%ld\n", printf(" %s, Version=%ld.%ld.%ld.%ld\n",
asm->a_name, assembly->a_name,
asm->a_version.v_major, assembly->a_version.v_major,
asm->a_version.v_minor, assembly->a_version.v_minor,
asm->a_version.v_build, assembly->a_version.v_build,
asm->a_version.v_revision); assembly->a_version.v_revision);
fx_bst_node *cur_node = fx_bst_first(&asm->a_types.m_entries); fx_bst_node *cur_node = fx_bst_first(&assembly->a_types.m_entries);
while (cur_node) { while (cur_node) {
struct map_entry *entry struct map_entry *entry
= fx_unbox(struct map_entry, cur_node, e_node); = fx_unbox(struct map_entry, cur_node, e_node);
@@ -187,19 +287,116 @@ static void assembly_dump(struct fx_assembly_p *asm)
} }
} }
static fx_iterator *assembly_get_types(const struct fx_assembly_p *assembly)
{
fx_assembly_type_iterator *it
= fx_object_create(fx_assembly_type_iterator_get_type());
if (!it) {
return NULL;
}
struct fx_assembly_type_iterator_p *it_p = fx_object_get_private(
it,
fx_assembly_type_iterator_get_type());
struct map_item *item = map_first_item(&assembly->a_types);
if (!item) {
fx_iterator_set_status(it, FX_ERR_NO_DATA);
return it;
}
it_p->it_item = fx_unbox(struct type, item, e_map_item);
const fx_type *ty = fx_type_get_by_id(it_p->it_item->e_type);
it_p->it_value = FX_VALUE_OBJECT_REF(ty);
return it;
}
static const char *assembly_get_name(const struct fx_assembly_p *assembly)
{
return assembly->a_name;
}
static void assembly_get_version(
const struct fx_assembly_p *assembly,
long *out_major,
long *out_minor,
long *out_build,
long *out_revision)
{
if (out_major) {
*out_major = assembly->a_version.v_major;
}
if (out_minor) {
*out_minor = assembly->a_version.v_minor;
}
if (out_build) {
*out_build = assembly->a_version.v_build;
}
if (out_revision) {
*out_revision = assembly->a_version.v_revision;
}
}
/*** PUBLIC FUNCTIONS *********************************************************/ /*** PUBLIC FUNCTIONS *********************************************************/
void fx_assembly_set_name(fx_assembly *asm, const char *name) fx_iterator *fx_assembly_get_all(void)
{
fx_assembly_iterator *it
= fx_object_create(fx_assembly_iterator_get_type());
if (!it) {
return NULL;
}
struct fx_assembly_iterator_p *it_p
= fx_object_get_private(it, fx_assembly_iterator_get_type());
it_p->it_cur = fx_namemap_first(&assembly_map);
if (!it_p->it_cur) {
fx_iterator_set_status(it, FX_ERR_NO_DATA);
return it;
}
struct fx_assembly_p *assembly
= fx_unbox(struct fx_assembly_p, it_p->it_cur, a_entry);
it_p->it_value = FX_VALUE_OBJECT_REF(assembly->a_self);
return it;
}
const fx_assembly *fx_assembly_get_by_name(const char *name)
{
const fx_namemap_entry *entry = fx_namemap_get(&assembly_map, name);
if (!entry) {
return NULL;
}
struct fx_assembly_p *assembly
= fx_unbox(struct fx_assembly_p, entry, a_entry);
return assembly->a_self;
}
fx_status fx_assembly_register(fx_assembly *assembly)
{
FX_CLASS_DISPATCH_STATIC_0(
FX_REFLECTION_TYPE_ASSEMBLY,
assembly_register,
assembly);
}
void fx_assembly_set_name(fx_assembly *assembly, const char *name)
{ {
FX_CLASS_DISPATCH_STATIC_V( FX_CLASS_DISPATCH_STATIC_V(
FX_REFLECTION_TYPE_ASSEMBLY, FX_REFLECTION_TYPE_ASSEMBLY,
assembly_set_name, assembly_set_name,
asm, assembly,
name); name);
} }
void fx_assembly_set_version( void fx_assembly_set_version(
fx_assembly *asm, fx_assembly *assembly,
long major, long major,
long minor, long minor,
long build, long build,
@@ -208,7 +405,7 @@ void fx_assembly_set_version(
FX_CLASS_DISPATCH_STATIC_V( FX_CLASS_DISPATCH_STATIC_V(
FX_REFLECTION_TYPE_ASSEMBLY, FX_REFLECTION_TYPE_ASSEMBLY,
assembly_set_version, assembly_set_version,
asm, assembly,
major, major,
minor, minor,
build, build,
@@ -216,49 +413,190 @@ void fx_assembly_set_version(
} }
void fx_assembly_add_type( void fx_assembly_add_type(
fx_assembly *asm, fx_assembly *assembly,
const char *full_name, const char *full_name,
fx_type type_id) fx_type_id type_id)
{ {
FX_CLASS_DISPATCH_STATIC_V( FX_CLASS_DISPATCH_STATIC_V(
FX_REFLECTION_TYPE_ASSEMBLY, FX_REFLECTION_TYPE_ASSEMBLY,
assembly_add_type, assembly_add_type,
asm, assembly,
full_name, full_name,
type_id); type_id);
} }
void fx_assembly_dump(const fx_assembly *asm) fx_iterator *fx_assembly_get_types(const fx_assembly *assembly)
{
FX_CLASS_DISPATCH_STATIC_0(
FX_REFLECTION_TYPE_ASSEMBLY,
assembly_get_types,
assembly);
}
const char *fx_assembly_get_name(const fx_assembly *assembly)
{
FX_CLASS_DISPATCH_STATIC_0(
FX_REFLECTION_TYPE_ASSEMBLY,
assembly_get_name,
assembly);
}
void fx_assembly_get_version(
const fx_assembly *assembly,
long *out_major,
long *out_minor,
long *out_build,
long *out_revision)
{
FX_CLASS_DISPATCH_STATIC_V(
FX_REFLECTION_TYPE_ASSEMBLY,
assembly_get_version,
assembly,
out_major,
out_minor,
out_build,
out_revision);
}
void fx_assembly_dump(const fx_assembly *assembly)
{ {
FX_CLASS_DISPATCH_STATIC_0( FX_CLASS_DISPATCH_STATIC_0(
FX_REFLECTION_TYPE_ASSEMBLY, FX_REFLECTION_TYPE_ASSEMBLY,
assembly_dump, assembly_dump,
asm); assembly);
} }
/*** VIRTUAL FUNCTIONS ********************************************************/ /*** VIRTUAL FUNCTIONS ********************************************************/
static void assembly_init(fx_object *obj, void *priv) static void assembly_init(fx_object *obj, void *priv)
{ {
struct fx_assembly_p *assembly = priv;
assembly->a_self = obj;
} }
static void assembly_fini(fx_object *obj, void *priv) static void assembly_fini(fx_object *obj, void *priv)
{ {
} }
/*** ITERATOR DEFINITION ******************************************************/
static enum fx_status type_iterator_move_next(const fx_iterator *obj)
{
struct fx_assembly_type_iterator_p *it = fx_object_get_private(
obj,
fx_assembly_type_iterator_get_type());
if (!it->it_item) {
return FX_ERR_NO_DATA;
}
fx_value_unset(&it->it_value);
struct map_item *next = map_item_next(&it->it_item->e_map_item);
it->it_item = fx_unbox(struct type, next, e_map_item);
if (!it->it_item) {
return FX_ERR_NO_DATA;
}
const fx_type *ty = fx_type_get_by_id(it->it_item->e_type);
it->it_value = FX_VALUE_OBJECT_REF(ty);
return FX_SUCCESS;
}
static const fx_value *type_iterator_get_value(const fx_iterator *obj)
{
struct fx_assembly_type_iterator_p *it = fx_object_get_private(
obj,
fx_assembly_type_iterator_get_type());
if (!it->it_item) {
return NULL;
}
return &it->it_value;
}
static enum fx_status assembly_iterator_move_next(const fx_iterator *obj)
{
struct fx_assembly_iterator_p *it
= fx_object_get_private(obj, fx_assembly_iterator_get_type());
if (!it->it_cur) {
return FX_ERR_NO_DATA;
}
fx_value_unset(&it->it_value);
it->it_cur = fx_namemap_next(&assembly_map, it->it_cur);
if (!it->it_cur) {
return FX_ERR_NO_DATA;
}
struct fx_assembly_p *assembly
= fx_unbox(struct fx_assembly_p, it->it_cur, a_entry);
it->it_value = FX_VALUE_OBJECT_REF(assembly->a_self);
return FX_SUCCESS;
}
static const fx_value *assembly_iterator_get_value(const fx_iterator *obj)
{
struct fx_assembly_iterator_p *it
= fx_object_get_private(obj, fx_assembly_iterator_get_type());
if (!it->it_cur) {
return NULL;
}
return &it->it_value;
}
/*** CLASS DEFINITION *********************************************************/ /*** CLASS DEFINITION *********************************************************/
// ---- fx_string DEFINITION FX_TYPE_CLASS_BEGIN(fx_assembly)
FX_TYPE_CLASS_DEFINITION_BEGIN(fx_assembly) FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL; FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT) FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_DEFINITION_END(fx_assembly) FX_TYPE_CLASS_END(fx_assembly)
FX_TYPE_DEFINITION_BEGIN(fx_assembly) FX_TYPE_DEFINITION_BEGIN(fx_assembly)
FX_TYPE_ID(0xf6690c30, 0x6642, 0x42f0, 0xb79f, 0xe2baf3684b1b); FX_TYPE_ID(0xf6690c30, 0x6642, 0x42f0, 0xb79f, 0xe2baf3684b1b);
FX_TYPE_NAME("fx.reflection.assembly");
FX_TYPE_CLASS(fx_assembly_class); FX_TYPE_CLASS(fx_assembly_class);
FX_TYPE_INSTANCE_PRIVATE(struct fx_assembly_p); FX_TYPE_INSTANCE_PRIVATE(struct fx_assembly_p);
FX_TYPE_INSTANCE_INIT(assembly_init); FX_TYPE_INSTANCE_INIT(assembly_init);
FX_TYPE_INSTANCE_FINI(assembly_fini); FX_TYPE_INSTANCE_FINI(assembly_fini);
FX_TYPE_DEFINITION_END(fx_assembly) FX_TYPE_DEFINITION_END(fx_assembly)
FX_TYPE_CLASS_BEGIN(fx_assembly_type_iterator)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterator, FX_TYPE_ITERATOR)
FX_INTERFACE_ENTRY(it_move_next) = type_iterator_move_next;
FX_INTERFACE_ENTRY(it_get_value) = type_iterator_get_value;
FX_TYPE_VTABLE_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR)
FX_TYPE_CLASS_END(fx_assembly_type_iterator)
FX_TYPE_DEFINITION_BEGIN(fx_assembly_type_iterator)
FX_TYPE_ID(0x74ecb8df, 0x155d, 0x4e45, 0x96a6, 0x0f71e3ea0a1e);
FX_TYPE_NAME("fx.reflection.assembly.type_iterator");
FX_TYPE_EXTENDS(FX_TYPE_ITERATOR);
FX_TYPE_CLASS(fx_assembly_type_iterator_class);
FX_TYPE_INSTANCE_PRIVATE(struct fx_assembly_type_iterator_p);
FX_TYPE_DEFINITION_END(fx_assembly_type_iterator)
FX_TYPE_CLASS_BEGIN(fx_assembly_iterator)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterator, FX_TYPE_ITERATOR)
FX_INTERFACE_ENTRY(it_move_next) = assembly_iterator_move_next;
FX_INTERFACE_ENTRY(it_get_value) = assembly_iterator_get_value;
FX_TYPE_VTABLE_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR)
FX_TYPE_CLASS_END(fx_assembly_iterator)
FX_TYPE_DEFINITION_BEGIN(fx_assembly_iterator)
FX_TYPE_ID(0x391f8d65, 0x9baf, 0x4941, 0xbdbe, 0xe739226f8947);
FX_TYPE_NAME("fx.reflection.assembly.iterator");
FX_TYPE_EXTENDS(FX_TYPE_ITERATOR);
FX_TYPE_CLASS(fx_assembly_iterator_class);
FX_TYPE_INSTANCE_PRIVATE(struct fx_assembly_iterator_p);
FX_TYPE_DEFINITION_END(fx_assembly_iterator)
+246
View File
@@ -0,0 +1,246 @@
#include <fx/macros.h>
#include <fx/reflection/function.h>
#include <fx/string.h>
#include <fx/value-type.h>
#include <fx/value.h>
#include <platform/callvm.h>
struct fx_function_p {
char *func_name;
fx_function_flags func_flags;
fx_function_impl func_impl;
fx_type_id func_return_type;
fx_type_id *func_arg_types;
/* number of explicit arguments that the function takes.
* if the FX_FUNCTION_F_VARARG flag is set, the function supports a
* variable number of arguments, which may be handled differently by the
* ABI */
size_t func_nr_args;
fx_value *func_bound_args;
size_t func_nr_bound_args, func_max_bound_args;
};
/*** PRIVATE FUNCTIONS ********************************************************/
static const char *function_get_name(const struct fx_function_p *func)
{
return func->func_name;
}
static fx_status function_bind(
struct fx_function_p *func,
fx_value *args,
size_t nr_args)
{
if (func->func_nr_bound_args) {
fx_value_unset_array(
func->func_bound_args,
func->func_nr_bound_args);
}
if (!args || !nr_args) {
func->func_nr_bound_args = 0;
return FX_SUCCESS;
}
if (nr_args > func->func_max_bound_args) {
fx_value *buf = realloc(
func->func_bound_args,
nr_args * sizeof *buf);
if (!buf) {
return FX_ERR_NO_MEMORY;
}
func->func_bound_args = buf;
func->func_max_bound_args = nr_args;
}
fx_value_copy_array(func->func_bound_args, args, nr_args);
func->func_nr_bound_args = nr_args;
return FX_SUCCESS;
}
static void push_arg(
const fx_value *arg,
fx_type_id param_type,
struct callvm *vm)
{
if (!param_type) {
callvm_push(vm, arg);
return;
}
unsigned int param_value_type = __fx_type_get_value_type(param_type);
if (param_value_type == __FX_VALUE_TYPE_CSTR
&& fx_value_is_type(arg, FX_TYPE_STRING)) {
fx_object *obj;
fx_value_get_object(arg, &obj);
fx_value cstr = FX_CSTR(fx_string_get_cstr(obj));
callvm_push(vm, &cstr);
return;
}
callvm_push(vm, arg);
}
static fx_status function_invoke(
const struct fx_function_p *func,
const fx_value *args,
size_t nr_args,
fx_value *return_value)
{
size_t total_args = nr_args + func->func_nr_bound_args;
if (total_args < func->func_nr_args) {
return FX_ERR_INVALID_ARGUMENT;
}
if (total_args > func->func_nr_args
&& !(func->func_flags & FX_FUNCTION_F_VARARG)) {
return FX_ERR_INVALID_ARGUMENT;
}
size_t nr_fixed_args = func->func_nr_args;
size_t nr_var_args = total_args - nr_fixed_args;
struct callvm vm = {0};
callvm_reset(&vm, nr_fixed_args);
size_t param_index = 0;
for (size_t i = 0; i < func->func_nr_bound_args; i++) {
const fx_value *arg = &func->func_bound_args[i];
fx_type_id param_type = NULL;
if (param_index < func->func_nr_args) {
param_type = func->func_arg_types[param_index];
}
push_arg(arg, param_type, &vm);
param_index++;
}
for (size_t i = 0; i < nr_args; i++) {
const fx_value *arg = &args[i];
fx_type_id param_type = NULL;
if (param_index < func->func_nr_args) {
param_type = func->func_arg_types[param_index];
}
push_arg(arg, param_type, &vm);
param_index++;
}
*return_value = callvm_invoke(
&vm,
func->func_impl,
func->func_return_type);
callvm_reset(&vm, 0);
return FX_SUCCESS;
}
/*** PUBLIC FUNCTIONS *********************************************************/
fx_function *fx_function_create(
const char *name,
fx_function_flags flags,
fx_function_impl impl,
const fx_type_id *args,
size_t nr_args,
fx_type_id return_type)
{
fx_function *func = fx_object_create(FX_REFLECTION_TYPE_FUNCTION);
if (!func) {
return NULL;
}
struct fx_function_p *p = fx_object_get_private(
func,
FX_REFLECTION_TYPE_FUNCTION);
if (nr_args == 1 && args[0] == NULL) {
nr_args = 0;
}
p->func_name = fx_strdup(name);
p->func_flags = flags;
p->func_impl = impl;
p->func_nr_args = nr_args;
p->func_return_type = return_type;
p->func_arg_types = calloc(nr_args, sizeof *args);
for (size_t i = 0; i < nr_args; i++) {
p->func_arg_types[i] = args[i];
}
return func;
}
const char *fx_function_get_name(const fx_function *func)
{
FX_CLASS_DISPATCH_STATIC_0(
FX_REFLECTION_TYPE_FUNCTION,
function_get_name,
func);
}
fx_status fx_function_bind(fx_function *func, fx_value *args, size_t nr_args)
{
FX_CLASS_DISPATCH_STATIC(
FX_REFLECTION_TYPE_FUNCTION,
function_bind,
func,
args,
nr_args);
}
fx_status fx_function_invoke(
const fx_function *func,
const fx_value *args,
size_t nr_args,
fx_value *return_value)
{
FX_CLASS_DISPATCH_STATIC(
FX_REFLECTION_TYPE_FUNCTION,
function_invoke,
func,
args,
nr_args,
return_value);
}
/*** VIRTUAL FUNCTIONS
* ********************************************************/
static void function_init(fx_object *obj, void *priv)
{
}
static void function_fini(fx_object *obj, void *priv)
{
struct fx_function_p *p = priv;
if (p->func_name) {
free(p->func_name);
}
if (p->func_arg_types) {
free(p->func_arg_types);
}
}
/*** CLASS DEFINITION *********************************************************/
FX_TYPE_CLASS_BEGIN(fx_function)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_END(fx_function)
FX_TYPE_DEFINITION_BEGIN(fx_function)
FX_TYPE_ID(0x09e40174, 0x7443, 0x486e, 0xad21, 0xcc9374762e7e);
FX_TYPE_NAME("fx.reflection.function");
FX_TYPE_CLASS(fx_function_class);
FX_TYPE_INSTANCE_PRIVATE(struct fx_function_p);
FX_TYPE_INSTANCE_INIT(function_init);
FX_TYPE_INSTANCE_FINI(function_fini);
FX_TYPE_DEFINITION_END(fx_function)
+21 -8
View File
@@ -1,6 +1,7 @@
#ifndef FX_REFLECTION_ASSEMBLY_H_ #ifndef FX_REFLECTION_ASSEMBLY_H_
#define FX_REFLECTION_ASSEMBLY_H_ #define FX_REFLECTION_ASSEMBLY_H_
#include <fx/iterator.h>
#include <fx/macros.h> #include <fx/macros.h>
FX_DECLS_BEGIN; FX_DECLS_BEGIN;
@@ -12,26 +13,38 @@ FX_DECLARE_TYPE(fx_assembly);
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_assembly) FX_TYPE_CLASS_DECLARATION_BEGIN(fx_assembly)
FX_TYPE_CLASS_DECLARATION_END(fx_assembly) FX_TYPE_CLASS_DECLARATION_END(fx_assembly)
FX_API fx_type fx_assembly_get_type(); FX_API fx_type_id fx_assembly_get_type();
FX_TYPE_DEFAULT_CONSTRUCTOR(fx_assembly, FX_REFLECTION_TYPE_ASSEMBLY); FX_TYPE_DEFAULT_CONSTRUCTOR(fx_assembly, FX_REFLECTION_TYPE_ASSEMBLY);
FX_API void fx_assembly_set_name(fx_assembly *asm, const char *name); FX_API fx_iterator *fx_assembly_get_all(void);
FX_API const fx_assembly *fx_assembly_get_by_name(const char *name);
FX_API fx_status fx_assembly_register(fx_assembly *assembly);
FX_API void fx_assembly_set_name(fx_assembly *assembly, const char *name);
FX_API void fx_assembly_set_version( FX_API void fx_assembly_set_version(
fx_assembly *asm, fx_assembly *assembly,
long major, long major,
long minor, long minor,
long build, long build,
long revision); long revision);
FX_API void fx_assembly_add_type( FX_API void fx_assembly_add_type(
fx_assembly *asm, fx_assembly *assembly,
const char *full_name, const char *full_name,
fx_type type_id); fx_type_id type_id);
FX_API fx_iterator *fx_assembly_get_types(const fx_assembly *assembly);
FX_API void fx_assembly_dump(const fx_assembly *asm); FX_API const char *fx_assembly_get_name(const fx_assembly *assembly);
FX_API void fx_assembly_get_version(
const fx_assembly *assembly,
long *out_major,
long *out_minor,
long *out_build,
long *out_revision);
FX_DECLS_END FX_API void fx_assembly_dump(const fx_assembly *assembly);
;
FX_DECLS_END;
#endif #endif
@@ -0,0 +1,48 @@
#ifndef FX_REFLECTION_FUNCTION_H_
#define FX_REFLECTION_FUNCTION_H_
#include <fx/macros.h>
#include <fx/status.h>
#include <fx/value.h>
FX_DECLS_BEGIN;
#define FX_REFLECTION_TYPE_FUNCTION (fx_function_get_type())
typedef enum fx_function_flags {
FX_FUNCTION_F_NONE = 0x00u,
FX_FUNCTION_F_STATIC = 0x01u,
FX_FUNCTION_F_VIRTUAL = 0x02u,
FX_FUNCTION_F_VARARG = 0x04u,
FX_FUNCTION_F_CONSTRUCTOR = 0x08u,
} fx_function_flags;
typedef void (*fx_function_impl)();
FX_DECLARE_TYPE(fx_function);
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_function)
FX_TYPE_CLASS_DECLARATION_END(fx_function)
FX_API fx_type_id fx_function_get_type();
FX_API const char *fx_function_get_name(const fx_function *func);
FX_API fx_function *fx_function_create(
const char *name,
fx_function_flags flags,
fx_function_impl impl,
const fx_type_id *args,
size_t nr_args,
fx_type_id return_type);
FX_API fx_status
fx_function_bind(fx_function *func, fx_value *args, size_t nr_args);
FX_API fx_status fx_function_invoke(
const fx_function *func,
const fx_value *args,
size_t nr_args,
fx_value *return_value);
FX_DECLS_END;
#endif
@@ -0,0 +1,38 @@
#ifndef FX_REFLECTION_PROPERTY_H_
#define FX_REFLECTION_PROPERTY_H_
#include <fx/macros.h>
#include <fx/value.h>
FX_DECLS_BEGIN;
#define FX_REFLECTION_TYPE_PROPERTY (fx_property_get_type())
FX_DECLARE_TYPE(fx_property)
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_property)
FX_TYPE_CLASS_DECLARATION_END(fx_property)
typedef fx_status (
*fx_property_getter)(const fx_value *, const fx_property *, fx_value *);
typedef fx_status (
*fx_property_setter)(fx_value *, const fx_property *, fx_value *);
FX_API fx_type_id fx_property_get_type(void);
FX_API fx_property *fx_property_create(
const char *name,
fx_property_getter get,
fx_property_setter set);
FX_API const char *fx_property_get_name(const fx_property *prop);
FX_API fx_status fx_property_get_value(
const fx_property *prop,
const fx_value *container,
fx_value *out);
FX_API fx_status fx_property_set_value(
const fx_property *prop,
fx_value *container,
fx_value *new_value);
#endif
@@ -0,0 +1,46 @@
#ifndef FX_REFLECTION_TYPE_H_
#define FX_REFLECTION_TYPE_H_
#include <fx/iterator.h>
#include <fx/macros.h>
#include <fx/reflection/assembly.h>
#include <fx/reflection/function.h>
#include <fx/reflection/property.h>
FX_DECLS_BEGIN;
struct fx_type_info;
#define FX_REFLECTION_TYPE_TYPE (fx_type_get_type())
FX_DECLARE_TYPE(fx_type);
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_type)
FX_TYPE_CLASS_DECLARATION_END(fx_type)
FX_API fx_type_id fx_type_get_type();
FX_API const fx_assembly *fx_type_get_assembly(const fx_type *ty);
FX_API fx_status fx_type_set_assembly(fx_type *ty, fx_assembly *assembly);
FX_API const char *fx_type_get_name(const fx_type *ty);
FX_API fx_type_flags fx_type_get_flags(const fx_type *ty);
FX_API const fx_function *fx_type_get_function(
const fx_type *ty,
const char *name);
FX_API fx_iterator *fx_type_get_functions(const fx_type *ty);
FX_API const fx_property *fx_type_get_property(
const fx_type *ty,
const char *name);
FX_API fx_iterator *fx_type_get_properties(const fx_type *ty);
FX_API const fx_type *fx_type_get_by_id(fx_type_id id);
FX_API const fx_type *fx_type_get_by_name(const char *name);
FX_API fx_type_id fx_type_get_id(const fx_type *ty);
FX_API const fx_type *fx_type_get_parent(const fx_type *ty);
FX_API void *fx_type_get_interface(const fx_type *ty, fx_type_id interface_id);
FX_API fx_type *__fx_type_create(struct fx_type_info *opaque);
#endif
+140
View File
@@ -0,0 +1,140 @@
#include <fx/macros.h>
#include <fx/reflection/property.h>
#include <fx/string.h>
#include <fx/value-type.h>
#include <fx/value.h>
#include <platform/callvm.h>
struct fx_property_p {
fx_property *prop_self;
char *prop_name;
fx_property_getter prop_getter;
fx_property_setter prop_setter;
};
/*** PRIVATE FUNCTIONS ********************************************************/
static const char *property_get_name(const struct fx_property_p *prop)
{
return prop->prop_name;
}
static fx_status property_get_value(
const struct fx_property_p *prop,
const fx_value *container,
fx_value *out)
{
if (!prop->prop_getter) {
return FX_ERR_NOT_SUPPORTED;
}
return prop->prop_getter(container, prop->prop_self, out);
}
static fx_status property_set_value(
const struct fx_property_p *prop,
fx_value *container,
fx_value *new_value)
{
if (!prop->prop_setter) {
return FX_ERR_NOT_SUPPORTED;
}
return prop->prop_setter(container, prop->prop_self, new_value);
}
/*** PUBLIC FUNCTIONS *********************************************************/
fx_property *fx_property_create(
const char *name,
fx_property_getter getter,
fx_property_setter setter)
{
fx_property *prop = fx_object_create(FX_REFLECTION_TYPE_PROPERTY);
if (!prop) {
return NULL;
}
struct fx_property_p *p = fx_object_get_private(
prop,
FX_REFLECTION_TYPE_PROPERTY);
p->prop_name = fx_strdup(name);
if (!p->prop_name) {
fx_property_unref(prop);
return NULL;
}
p->prop_self = prop;
p->prop_getter = getter;
p->prop_setter = setter;
return prop;
}
const char *fx_property_get_name(const fx_property *prop)
{
FX_CLASS_DISPATCH_STATIC_0(
FX_REFLECTION_TYPE_PROPERTY,
property_get_name,
prop);
}
fx_status fx_property_get_value(
const fx_property *prop,
const fx_value *container,
fx_value *out)
{
FX_CLASS_DISPATCH_STATIC(
FX_REFLECTION_TYPE_PROPERTY,
property_get_value,
prop,
container,
out);
}
fx_status fx_property_set_value(
const fx_property *prop,
fx_value *container,
fx_value *new_value)
{
FX_CLASS_DISPATCH_STATIC(
FX_REFLECTION_TYPE_PROPERTY,
property_set_value,
prop,
container,
new_value);
}
/*** VIRTUAL FUNCTIONS ********************************************************/
static void property_init(fx_object *obj, void *priv)
{
}
static void property_fini(fx_object *obj, void *priv)
{
struct fx_property_p *p = priv;
if (p->prop_name) {
free(p->prop_name);
p->prop_name = NULL;
}
}
/*** CLASS DEFINITION
* *********************************************************/
FX_TYPE_CLASS_BEGIN(fx_property)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_END(fx_property)
FX_TYPE_DEFINITION_BEGIN(fx_property)
FX_TYPE_ID(0x7007a5ef, 0x3caf, 0x4215, 0x8c0b, 0xd54bf8195624);
FX_TYPE_NAME("fx.reflection.property");
FX_TYPE_CLASS(fx_property_class);
FX_TYPE_INSTANCE_PRIVATE(struct fx_property_p);
FX_TYPE_INSTANCE_INIT(property_init);
FX_TYPE_INSTANCE_FINI(property_fini);
FX_TYPE_DEFINITION_END(fx_property)
+92
View File
@@ -0,0 +1,92 @@
.global _callvm_invoke_i
.align 4
# x0 = (function ptr) impl
# x1 = (struct callvm) context
_callvm_invoke_i:
# save the stack frame and link pointer
stp x29, x30, [sp, #-16]!
mov x29, sp
# preserve callee-saved registers
stp x19, x20, [sp, #-16]!
# store function pointer and callvm context for later
mov x19, x0
mov x20, x1
# First, set up the variable arguments
### calculate the amount of stack space needed for the varargs
# store the original stack pointer, so it can be restored later
mov x0, sp
str x0, [sp, #-16]!
# x8 = excess arg count
ldr x8, [x20, #32]
# x8 = excess arg buffer size (8 bytes per value)
lsl x8, x8, #3
# allocate the stack space
sub sp, sp, x8
# re-align the stack, and take a pointer to the newly-allocated buffer
mov x0, sp
and x0, x0, #0xFFFFFFFFFFFFFFF0
mov sp, x0
ldr x1, [x20, #176]
mov x2, x8
# memcpy(stack vararg buffer, callvm excess buffer, arg size)
bl _memcpy
# Next, set up the fixed integer arguments
ldr x0, [x20, #48]
ldr x1, [x20, #56]
ldr x2, [x20, #64]
ldr x3, [x20, #72]
ldr x4, [x20, #80]
ldr x5, [x20, #88]
ldr x6, [x20, #96]
ldr x7, [x20, #104]
# Finally, set up the fixed double arguments
ldr d0, [x20, #112]
ldr d1, [x20, #120]
ldr d2, [x20, #128]
ldr d3, [x20, #136]
ldr d4, [x20, #144]
ldr d5, [x20, #152]
ldr d6, [x20, #160]
ldr d7, [x20, #168]
# call the function implementation
blr x19
# restore the saved stack pointer, and de-allocate the stack varargs buffer
ldr x9, [x29, #-32]
mov sp, x9
# restore callee-saved registers
ldp x19, x20, [sp], #16
# restore the saved stack frame and link pointer
ldp x29, x30, [sp], #16
ret
.global _callvm_invoke_d
.align 4
# x0 = (function ptr) impl
# x1 = (struct callvm) context
_callvm_invoke_d:
b _callvm_invoke_i
.global _callvm_invoke_v
.align 4
# x0 = (function ptr) impl
# x1 = (struct callvm) context
_callvm_invoke_v:
b _callvm_invoke_i
+240
View File
@@ -0,0 +1,240 @@
#include <fx/type.h>
#include <fx/value-type.h>
#include <fx/value.h>
#include <platform/callvm.h>
#include <stdlib.h>
#if 0
switch (arg->v_type.t_primitive) {
case FX_VALUE_TYPE_DOUBLE:
break;
default:
callvm_push_int(&vm, (uintptr_t)arg->v_pointer);
break;
}
#endif
void callvm_reset(struct callvm *vm, unsigned int max_fixed_args)
{
vm->vm_arg_int_count = 0;
vm->vm_arg_double_count = 0;
vm->vm_arg_fixed = max_fixed_args;
vm->vm_arg_excess_count = 0;
}
static void expand_excess(struct callvm *vm)
{
size_t new_capacity = vm->vm_arg_excess_max * 2;
if (!new_capacity) {
new_capacity = 4;
}
void *buf = realloc(
vm->vm_arg_excess,
new_capacity * sizeof *vm->vm_arg_excess);
if (!buf) {
return;
}
vm->vm_arg_excess = buf;
vm->vm_arg_excess_max = new_capacity;
}
static void push_excess(struct callvm *vm, uintptr_t value)
{
if (vm->vm_arg_excess_count + 1 > vm->vm_arg_excess_max) {
expand_excess(vm);
}
vm->vm_arg_excess[vm->vm_arg_excess_count++] = value;
vm->vm_arg_count++;
}
static void push_int(struct callvm *vm, uintptr_t value)
{
if (vm->vm_arg_int_count >= MAX_INT_ARGS) {
push_excess(vm, value);
return;
}
vm->vm_arg_int[vm->vm_arg_int_count++] = value;
vm->vm_arg_count++;
}
static void push_double(struct callvm *vm, double value)
{
if (vm->vm_arg_double_count >= MAX_DOUBLE_ARGS) {
push_excess(vm, *(uintptr_t *)&value);
return;
}
vm->vm_arg_double[vm->vm_arg_double_count++] = value;
vm->vm_arg_count++;
}
void callvm_push(struct callvm *vm, const fx_value *value)
{
if (vm->vm_arg_count >= vm->vm_arg_fixed) {
push_excess(vm, (uintptr_t)value->v_pointer);
return;
}
if (!fx_type_is_value_type(value->v_type)) {
push_int(vm, (uintptr_t)value->v_object);
return;
}
unsigned int value_type = __fx_type_get_value_type(value->v_type);
switch (value_type) {
case __FX_VALUE_TYPE_BOOL:
push_int(vm, value->v_bool);
break;
case __FX_VALUE_TYPE_I16:
push_int(vm, value->v_i16);
break;
case __FX_VALUE_TYPE_U16:
push_int(vm, value->v_u16);
break;
case __FX_VALUE_TYPE_I32:
push_int(vm, value->v_i32);
break;
case __FX_VALUE_TYPE_U32:
push_int(vm, value->v_u32);
break;
case __FX_VALUE_TYPE_I64:
push_int(vm, value->v_i64);
break;
case __FX_VALUE_TYPE_U64:
push_int(vm, value->v_u64);
break;
case __FX_VALUE_TYPE_IPTR:
push_int(vm, value->v_iptr);
break;
case __FX_VALUE_TYPE_UPTR:
push_int(vm, value->v_uptr);
break;
case __FX_VALUE_TYPE_SBYTE:
push_int(vm, value->v_sbyte);
break;
case __FX_VALUE_TYPE_BYTE:
push_int(vm, value->v_byte);
break;
case __FX_VALUE_TYPE_SHORT:
push_int(vm, value->v_short);
break;
case __FX_VALUE_TYPE_USHORT:
push_int(vm, value->v_ushort);
break;
case __FX_VALUE_TYPE_INT:
push_int(vm, value->v_int);
break;
case __FX_VALUE_TYPE_UINT:
push_int(vm, value->v_uint);
break;
case __FX_VALUE_TYPE_LONG:
push_int(vm, value->v_long);
break;
case __FX_VALUE_TYPE_ULONG:
push_int(vm, value->v_ulong);
break;
case __FX_VALUE_TYPE_LONGLONG:
push_int(vm, value->v_longlong);
break;
case __FX_VALUE_TYPE_ULONGLONG:
push_int(vm, value->v_ulonglong);
break;
case __FX_VALUE_TYPE_SIZE:
push_int(vm, value->v_size);
break;
case __FX_VALUE_TYPE_FLOAT:
push_double(vm, value->v_float);
break;
case __FX_VALUE_TYPE_DOUBLE:
push_double(vm, value->v_double);
break;
case __FX_VALUE_TYPE_CSTR:
push_int(vm, (uintptr_t)value->v_cstr);
break;
case __FX_VALUE_TYPE_POINTER:
push_int(vm, (uintptr_t)value->v_pointer);
break;
default:
break;
}
}
extern uintptr_t callvm_invoke_i(fx_function_impl impl, struct callvm *vm);
extern double callvm_invoke_d(fx_function_impl impl, struct callvm *vm);
extern void callvm_invoke_v(fx_function_impl impl, struct callvm *vm);
fx_value callvm_invoke(
struct callvm *vm,
fx_function_impl impl,
fx_type_id return_type)
{
if (!fx_type_is_value_type(return_type)) {
uintptr_t v = callvm_invoke_i(impl, vm);
fx_value result = {
.v_type = return_type,
.v_object = (fx_object *)v,
};
return result;
}
unsigned int value_type = __fx_type_get_value_type(return_type);
switch (value_type) {
case __FX_VALUE_TYPE_BOOL:
return FX_BOOL(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_I16:
return FX_I16(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_U16:
return FX_U16(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_I32:
return FX_I32(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_U32:
return FX_U32(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_I64:
return FX_I64(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_U64:
return FX_U64(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_IPTR:
return FX_IPTR(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_UPTR:
return FX_UPTR(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_SBYTE:
return FX_SBYTE(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_BYTE:
return FX_BYTE(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_SHORT:
return FX_SHORT(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_USHORT:
return FX_USHORT(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_INT:
return FX_INT(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_UINT:
return FX_UINT(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_LONG:
return FX_LONG(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_ULONG:
return FX_ULONG(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_LONGLONG:
return FX_LONGLONG(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_ULONGLONG:
return FX_ULONGLONG(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_SIZE:
return FX_SIZE(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_FLOAT:
return FX_FLOAT(callvm_invoke_d(impl, vm));
case __FX_VALUE_TYPE_DOUBLE:
return FX_DOUBLE(callvm_invoke_d(impl, vm));
case __FX_VALUE_TYPE_CSTR:
return FX_CSTR((const char *)callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_POINTER:
return FX_POINTER((void *)callvm_invoke_i(impl, vm));
default:
return FX_VALUE_EMPTY;
}
}
@@ -0,0 +1,43 @@
#ifndef FX_REFLECTION_DARWIN_ARM64_CALLVM_H_
#define FX_REFLECTION_DARWIN_ARM64_CALLVM_H_
#include <fx/reflection/function.h>
#include <fx/value.h>
#include <stdint.h>
#define CALLVM_INVOKE_PROTOTYPE(suffix) \
fx_value callvm_invoke_##suffix( \
struct callvm *vm, \
fx_function_impl impl)
#define MAX_FIXED_ARGS ((unsigned int)-1)
#define MAX_DOUBLE_ARGS 8
#define MAX_INT_ARGS 8
/* dyn-dispatch.S depends on the layout of this struct */
struct callvm {
uint64_t vm_arg_int_count;
uint64_t vm_arg_double_count;
/* any args pushed after this limit is reached will be stored in the
* excess buffer. used for calling varargs functions */
uint64_t vm_arg_count, vm_arg_fixed;
uint64_t vm_arg_excess_count;
uint64_t vm_arg_excess_max;
uintptr_t vm_arg_int[MAX_INT_ARGS];
double vm_arg_double[MAX_DOUBLE_ARGS];
uintptr_t *vm_arg_excess;
};
typedef fx_value (*callvm_invoke_impl)(struct callvm *, fx_function_impl);
extern void callvm_reset(struct callvm *vm, unsigned int max_fixed_args);
extern void callvm_push(struct callvm *vm, const fx_value *value);
extern fx_value callvm_invoke(
struct callvm *vm,
fx_function_impl impl,
fx_type_id return_type);
#endif
+95
View File
@@ -0,0 +1,95 @@
.extern memcpy
.type memcpy, @function
.global callvm_invoke_i
.type callvm_invoke_i, @function
# %rdi = (function ptr) impl
# %rsi = (struct callvm) context
callvm_invoke_i:
# save the stack frame pointer
push %rbp
mov %rsp, %rbp
# store function pointer for later
push %r12
push %r13
push %r14
push %r15
# move our parameters out of the way
mov %rdi, %r11
mov %rsi, %r12
# calculate the amount of stack space needed for the varargs
movq 32(%r12), %r13
shl $3, %r13
# allocate the stack space
push %rsp
sub %r13, %rsp
andq $0xFFFFFFFFFFFFFFF0, %rsp # re-align the stack
# copy the excess args to the stack
mov %rsp, %rdi
mov 160(%r12), %rsi
mov %r13, %rdx
call memcpy
# Next, set up the fixed integer arguments
movq 48(%r12), %rdi # int arg 0
movq 56(%r12), %rsi # int arg 1
movq 64(%r12), %rdx # int arg 2
movq 72(%r12), %rcx # int arg 3
movq 80(%r12), %r8 # int arg 4
movq 88(%r12), %r9 # int arg 5
# Finally, set up the fixed double arguments
movq 96(%r12), %xmm0 # double arg 0
movq 104(%r12), %xmm1 # double arg 1
movq 112(%r12), %xmm2 # double arg 2
movq 120(%r12), %xmm3 # double arg 3
movq 128(%r12), %xmm4 # double arg 4
movq 136(%r12), %xmm5 # double arg 5
movq 144(%r12), %xmm6 # double arg 6
movq 152(%r12), %xmm7 # double arg 7
# set the number of vararg double parameters
# as required by the ABI
mov 168(%r12), %rax
# call the function implementation
call *%r11
# Restore the stack pointer (deallocating the varargs buffer)
mov -40(%rbp), %rsp
# Restore callee-saved registers
pop %r15
pop %r14
pop %r13
pop %r12
# restore the saved stack frame
pop %rbp
ret
.global callvm_invoke_d
.type callvm_invoke_d, @function
# %rdi = (function ptr) impl
# %rsi = (struct callvm *) context
callvm_invoke_d:
jmp callvm_invoke_i
.global callvm_invoke_v
.type callvm_invoke_v, @function
# %rdi = (function ptr) impl
# %rsi = (struct callvm *) context
callvm_invoke_v:
jmp callvm_invoke_i

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