Compare commits

...

9 Commits

68 changed files with 1906 additions and 926 deletions
+4 -62
View File
@@ -1,64 +1,6 @@
BasedOnStyle: WebKit BasedOnStyle: WebKit
IndentWidth: 8 IndentWidth: 8
--- ---
Language: C
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: 999999999
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: ObjC Language: ObjC
DerivePointerAlignment: false DerivePointerAlignment: false
PointerAlignment: Right PointerAlignment: Right
@@ -85,7 +27,7 @@ AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: true AlwaysBreakBeforeMultilineStrings: true
AlwaysBreakTemplateDeclarations: Yes AlwaysBreakTemplateDeclarations: Yes
BinPackArguments: false BinPackArguments: false
BinPackParameters: true BinPackParameters: false
ExperimentalAutoDetectBinPacking: false ExperimentalAutoDetectBinPacking: false
BitFieldColonSpacing: Both BitFieldColonSpacing: Both
BreakBeforeBraces: Linux BreakBeforeBraces: Linux
@@ -143,7 +85,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
@@ -172,5 +114,5 @@ AttributeMacros:
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"
+10 -15
View File
@@ -9,9 +9,13 @@ set(CMAKE_C_EXTENSIONS OFF)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_SOURCE_DIR}/cmake) 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 file(GLOB _all_assemblies assemblies/*
fx.runtime LIST_DIRECTORIES TRUE)
fx.collections)
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})
@@ -38,17 +42,8 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/bin)
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/Templates.cmake) include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/Templates.cmake)
if ("fx.runtime" IN_LIST fx_assemblies) foreach (assembly ${fx_assemblies})
add_fx_assembly( add_subdirectory(assemblies/${assembly})
NAME fx.runtime endforeach (assembly)
NAMESPACES fx fx.reflection)
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) 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_ASSEMBLY_NAME("fx.cmdline");
FX_ASSEMBLY_VERSION(1, 0, 0, 0);
FX_ASSEMBLY_END()
+4
View File
@@ -0,0 +1,4 @@
add_fx_assembly(
NAME fx.collections
NAMESPACES fx.collections
DEPENDENCIES fx.runtime)
+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_ASSEMBLY_NAME("fx.compression");
FX_ASSEMBLY_VERSION(1, 0, 0, 0);
FX_ASSEMBLY_END()
+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_ASSEMBLY_NAME("fx.io");
FX_ASSEMBLY_VERSION(1, 0, 0, 0);
FX_ASSEMBLY_END()
+3
View File
@@ -0,0 +1,3 @@
add_fx_assembly(
NAME fx.runtime
NAMESPACES fx fx.reflection)
+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_ASSEMBLY_NAME("fx.serial");
FX_ASSEMBLY_VERSION(1, 0, 0, 0);
FX_ASSEMBLY_END()
+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_ASSEMBLY_NAME("fx.term");
FX_ASSEMBLY_VERSION(1, 0, 0, 0);
FX_ASSEMBLY_END()
+21 -25
View File
@@ -17,11 +17,10 @@ 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)
foreach (dir ${arg_NAMESPACES}) foreach (dir ${arg_NAMESPACES})
add_subdirectory(${dir}) add_subdirectory(../../${dir} ../../namespaces/${dir})
set(assembly_sources ${assembly_sources} ${namespace_sources}) set(assembly_sources ${assembly_sources} ${namespace_sources})
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,17 +38,8 @@ 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})
@@ -71,30 +61,36 @@ function(add_fx_assembly)
install(FILES ${headers} DESTINATION include/${assembly_path}) 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)
file(GLOB headers include/${namespace_path}/*.h) file(GLOB headers include/${namespace_path}/*.h)
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_libs ${internal_libs} PARENT_SCOPE)
set(internal_include_dirs ${internal_include_dirs} PARENT_SCOPE)
set(internal_defines ${internal_defines} PARENT_SCOPE)
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)
+20 -8
View File
@@ -1,7 +1,7 @@
#include "command.h" #include "command.h"
#include <fx/cmd.h> #include <fx/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, "]");
+133 -53
View File
@@ -1,8 +1,8 @@
#include "command.h" #include "command.h"
#include <fx/cmd.h> #include <fx/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;
} }
+142 -64
View File
@@ -1,8 +1,8 @@
#include "command.h" #include "command.h"
#include <fx/bst.h>
#include <fx/cmd.h> #include <fx/cmd.h>
#include <fx/core/bst.h> #include <fx/string.h>
#include <fx/ds/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,
+ 4; FX_STRLEN_IGNORE_ESC | FX_STRLEN_IGNORE_MOD)
+ 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,
+ 4; FX_STRLEN_IGNORE_ESC | FX_STRLEN_IGNORE_MOD)
+ 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,9 +721,11 @@ 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(
+ 4; str,
FX_STRLEN_IGNORE_ESC | FX_STRLEN_IGNORE_MOD)
+ 4;
if (len > desc_margin) { if (len > desc_margin) {
desc_margin = len; desc_margin = len;
@@ -705,10 +750,11 @@ 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,
+ 4; FX_STRLEN_IGNORE_ESC | FX_STRLEN_IGNORE_MOD)
+ 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, ' ');
len++; len++;
@@ -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,9 +787,11 @@ 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(
+ 4; str,
FX_STRLEN_IGNORE_ESC | FX_STRLEN_IGNORE_MOD)
+ 4;
if (len > desc_margin) { if (len > desc_margin) {
desc_margin = len; desc_margin = len;
@@ -765,10 +816,11 @@ 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,
+ 4; FX_STRLEN_IGNORE_ESC | FX_STRLEN_IGNORE_MOD)
+ 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, ' ');
len++; len++;
@@ -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;
+35 -17
View File
@@ -1,10 +1,10 @@
#ifndef _FX_COMMAND_H_ #ifndef _FX_COMMAND_H_
#define _FX_COMMAND_H_ #define _FX_COMMAND_H_
#include <fx/bst.h>
#include <fx/cmd.h> #include <fx/cmd.h>
#include <fx/core/bst.h> #include <fx/queue.h>
#include <fx/core/queue.h> #include <fx/string.h>
#include <fx/ds/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
+155 -93
View File
@@ -1,53 +1,64 @@
#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_arglist_option_iterator_is_valid(it); \ FX_COMMAND_INVALID_ID, \
(it)); \
fx_arglist_option_iterator_is_valid(it); \
fx_arglist_option_iterator_next(it)) fx_arglist_option_iterator_next(it))
#define fx_arglist_option_foreach_filtered(it, q, opt_id) \ #define fx_arglist_option_foreach_filtered(it, q, opt_id) \
for (int z__fx_unique_name() \ for (int z__fx_unique_name() \
= fx_arglist_option_iterator_begin(q, opt_id, (it)); \ = fx_arglist_option_iterator_begin(q, opt_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))
#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_usage *); \ fx_command_option *, \
FX_INIT(init_##id) \ fx_command_arg *, \
fx_command_usage *); \
FX_INIT(init_##id) \
{ \ { \
command_##id = fx_command_create(id); \ command_##id = fx_command_create(id); \
if ((parent_id) != FX_COMMAND_INVALID_ID) { \ if ((parent_id) != FX_COMMAND_INVALID_ID) { \
fx_command_set_parent(command_##id, parent_id); \ fx_command_set_parent(command_##id, parent_id); \
} \ } \
__init_##id(command_##id, NULL, NULL, NULL); \ __init_##id(command_##id, NULL, NULL, NULL); \
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))
@@ -56,41 +67,44 @@
#define FX_COMMAND_FLAGS(flags) fx_command_set_flags(this_cmd, (flags)) #define FX_COMMAND_FLAGS(flags) fx_command_set_flags(this_cmd, (flags))
#define FX_COMMAND_FUNCTION(fn) fx_command_set_callback(this_cmd, (fn)) #define FX_COMMAND_FUNCTION(fn) fx_command_set_callback(this_cmd, (fn))
#define FX_COMMAND_OPTION(id) \ #define FX_COMMAND_OPTION(id) \
fx_command_option *opt_##id = fx_command_add_option(this_cmd, (id)); \ fx_command_option *opt_##id = fx_command_add_option(this_cmd, (id)); \
this_opt = opt_##id; \ this_opt = opt_##id; \
if (this_opt) if (this_opt)
#define FX_COMMAND_OPTION_GEN(id) \ #define FX_COMMAND_OPTION_GEN(id) \
fx_command_option *z__fx_unique_name() \ fx_command_option *z__fx_unique_name() \
= fx_command_add_option(this_cmd, (id)); \ = fx_command_add_option(this_cmd, (id)); \
this_opt = z__fx_unique_name(); \ this_opt = z__fx_unique_name(); \
if (this_opt) if (this_opt)
#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_short_name(opt, 'h'); \ fx_command_option_set_description( \
fx_command_option_set_long_name(opt, "help"); \ opt, \
"Show this help message"); \
fx_command_option_set_short_name(opt, 'h'); \
fx_command_option_set_long_name(opt, "help"); \
} while (0) } while (0)
#define FX_OPTION_LONG_NAME(name) \ #define FX_OPTION_LONG_NAME(name) \
fx_command_option_set_long_name(this_opt, (name)) fx_command_option_set_long_name(this_opt, (name))
#define FX_OPTION_SHORT_NAME(name) \ #define FX_OPTION_SHORT_NAME(name) \
fx_command_option_set_short_name(this_opt, (name)) fx_command_option_set_short_name(this_opt, (name))
#define FX_OPTION_DESC(desc) fx_command_option_set_description(this_opt, (desc)) #define FX_OPTION_DESC(desc) fx_command_option_set_description(this_opt, (desc))
#define FX_OPTION_ARG(id) \ #define FX_OPTION_ARG(id) \
fx_command_arg *arg_##id = fx_command_option_add_arg(this_opt, (id)); \ fx_command_arg *arg_##id = fx_command_option_add_arg(this_opt, (id)); \
this_arg = arg_##id; \ this_arg = arg_##id; \
if (this_arg) if (this_arg)
#define FX_COMMAND_ARG(id) \ #define FX_COMMAND_ARG(id) \
fx_command_arg *arg_##id = fx_command_add_arg(this_cmd, (id)); \ fx_command_arg *arg_##id = fx_command_add_arg(this_cmd, (id)); \
this_arg = arg_##id; \ this_arg = arg_##id; \
if (this_arg) if (this_arg)
@@ -98,37 +112,38 @@
#define FX_ARG_DESC(desc) fx_command_arg_set_description(this_arg, (desc)) #define FX_ARG_DESC(desc) fx_command_arg_set_description(this_arg, (desc))
#define FX_ARG_NR_VALUES(nr_values) \ #define FX_ARG_NR_VALUES(nr_values) \
fx_command_arg_set_nr_values(this_arg, (nr_values)) fx_command_arg_set_nr_values(this_arg, (nr_values))
#define FX_ARG_ALLOWED_VALUES(...) \ #define FX_ARG_ALLOWED_VALUES(...) \
static const char *allowed_values[] = { \ static const char *allowed_values[] = { \
__VA_ARGS__, \ __VA_ARGS__, \
NULL, \ NULL, \
}; \ }; \
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() \
this_usage = z__fx_unique_name(); \ = fx_command_add_usage(this_cmd); \
this_usage = z__fx_unique_name(); \
if (this_usage) if (this_usage)
#define FX_COMMAND_USAGE_COMMAND(cmd_id) \ #define FX_COMMAND_USAGE_COMMAND(cmd_id) \
fx_command_usage_add_command(this_usage, cmd_id) fx_command_usage_add_command(this_usage, cmd_id)
#define FX_COMMAND_USAGE_COMMAND_PLACEHOLDER() \ #define FX_COMMAND_USAGE_COMMAND_PLACEHOLDER() \
fx_command_usage_add_command(this_usage, FX_COMMAND_INVALID_ID) fx_command_usage_add_command(this_usage, FX_COMMAND_INVALID_ID)
#define FX_COMMAND_USAGE_OPT(opt_id) \ #define FX_COMMAND_USAGE_OPT(opt_id) \
fx_command_usage_add_option(this_usage, opt_##opt_id) fx_command_usage_add_option(this_usage, opt_##opt_id)
#define FX_COMMAND_USAGE_OPT_PLACEHOLDER() \ #define FX_COMMAND_USAGE_OPT_PLACEHOLDER() \
fx_command_usage_add_option(this_usage, NULL) fx_command_usage_add_option(this_usage, NULL)
#define FX_COMMAND_USAGE_ARG(opt_id) \ #define FX_COMMAND_USAGE_ARG(opt_id) \
fx_command_usage_add_arg(this_usage, arg_##opt_id) fx_command_usage_add_arg(this_usage, arg_##opt_id)
#define FX_COMMAND_USAGE_ARG_PLACEHOLDER() \ #define FX_COMMAND_USAGE_ARG_PLACEHOLDER() \
fx_command_usage_add_arg(this_usage, NULL) fx_command_usage_add_arg(this_usage, NULL)
#define FX_COMMAND_OPTION_HELP ((uintptr_t)0xF0000001) #define FX_COMMAND_OPTION_HELP ((uintptr_t)0xF0000001)
@@ -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(
+27 -12
View File
@@ -1,7 +1,7 @@
#include "command.h" #include "command.h"
#include <fx/cmd.h> #include <fx/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);
+59 -28
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);
@@ -44,7 +48,7 @@ enum fx_status fx_arglist_report_missing_option(
} }
fx_err("required option `" F_YELLOW "%s" F_RESET "` was not specified.", fx_err("required option `" F_YELLOW "%s" F_RESET "` was not specified.",
fx_stringstream_ptr(opt_name)); fx_stringstream_ptr(opt_name));
fx_i("usage: %s", fx_string_get_cstr(opt_string)); fx_i("usage: %s", fx_string_get_cstr(opt_string));
fx_i("for more information, use `" F_YELLOW "--help" F_RESET "`"); fx_i("for more information, use `" F_YELLOW "--help" F_RESET "`");
@@ -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) {
@@ -94,17 +107,20 @@ 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
fx_string_get_cstr(opt_string)); "' accepts the following values:",
fx_string_get_cstr(opt_string));
} }
for (int i = 0; arg->arg_allowed_values[i]; i++) { for (int i = 0; arg->arg_allowed_values[i]; i++) {
@@ -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 -2
View File
@@ -1,2 +1 @@
export_fx_namespace_details( export_fx_namespace_details(fx.collections)
NAME fx.collections)
+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,15 +1,15 @@
#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>
#define COMPRESSOR_DISPATCH_STATIC(func, compressor, ...) \ #define COMPRESSOR_DISPATCH_STATIC(func, compressor, ...) \
do { \ do { \
struct compressor_data _compressor; \ struct compressor_data _compressor; \
enum fx_status status \ enum fx_status status \
= compressor_get_data(compressor, &_compressor); \ = compressor_get_data(compressor, &_compressor); \
if (!FX_OK(status)) { \ if (!FX_OK(status)) { \
return status; \ return status; \
} \ } \
return func(&_compressor, __VA_ARGS__); \ return func(&_compressor, __VA_ARGS__); \
@@ -17,9 +17,9 @@
#define COMPRESSOR_DISPATCH_STATIC_0(func, compressor) \ #define COMPRESSOR_DISPATCH_STATIC_0(func, compressor) \
do { \ do { \
struct compressor_data _compressor; \ struct compressor_data _compressor; \
enum fx_status status \ enum fx_status status \
= compressor_get_data(compressor, &_compressor); \ = compressor_get_data(compressor, &_compressor); \
if (!FX_OK(status)) { \ if (!FX_OK(status)) { \
return status; \ return status; \
} \ } \
return func(&_compressor); \ return func(&_compressor); \
@@ -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 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 ********************************************************/
+183 -58
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 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;
} }
@@ -735,7 +808,7 @@ enum fx_status fx_cstream_open(
p->s_endpoint = endpoint; p->s_endpoint = endpoint;
cfg->s_mode = (mode == FX_COMPRESSOR_MODE_COMPRESS) ? FX_STREAM_WRITE cfg->s_mode = (mode == FX_COMPRESSOR_MODE_COMPRESS) ? FX_STREAM_WRITE
: FX_STREAM_READ; : FX_STREAM_READ;
p->s_in = fx_ringbuffer_create(inbuf_size + 1); p->s_in = fx_ringbuffer_create(inbuf_size + 1);
if (!FX_OK(status)) { if (!FX_OK(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 ********************************************************/
+38 -13
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);
@@ -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;
@@ -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 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;
@@ -19,28 +19,37 @@ FX_TYPE_CLASS_DECLARATION_END(fx_cstream)
FX_API fx_type fx_cstream_get_type(void); FX_API fx_type 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 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;
@@ -19,7 +19,9 @@ FX_TYPE_CLASS_DECLARATION_END(fx_compressor)
FX_API fx_type fx_zstd_compressor_get_type(void); FX_API fx_type 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 -3
View File
@@ -1,3 +1 @@
include(../cmake/Templates.cmake) export_fx_namespace_details(fx.io)
add_fx_module(NAME io DEPENDENCIES core ds)
+22 -11
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)
@@ -49,7 +49,9 @@ FX_API fx_type fx_directory_get_type(void);
FX_API fx_type fx_directory_iterator_get_type(void); FX_API fx_type 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;
+21 -11
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;
@@ -54,25 +54,35 @@ typedef struct fx_file_info {
FX_API fx_type fx_file_get_type(void); FX_API fx_type 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;
+9 -6
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;
@@ -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);
+104 -36
View File
@@ -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>
@@ -58,11 +58,14 @@ 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 +84,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 +104,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 +124,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 +144,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 +165,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 +200,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 +228,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 +241,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 +252,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 +272,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 +291,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();
@@ -307,11 +328,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,7 +354,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);
@@ -353,23 +384,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);
@@ -378,31 +418,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 ********************************************************/
@@ -487,7 +551,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
@@ -556,7 +621,9 @@ static fx_iterator *iterator_begin(fx_object *obj)
static const fx_iterator *iterator_cbegin(const fx_object *obj) static const fx_iterator *iterator_cbegin(const fx_object *obj)
{ {
return fx_directory_begin((fx_object *)obj, FX_DIRECTORY_ITERATE_PARENT_FIRST); 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)
@@ -612,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);
+100 -29
View File
@@ -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 ********************************************************/
+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)
{ {
+29 -12
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)
+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
+124 -60
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>
@@ -60,7 +60,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 +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,
@@ -103,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,
@@ -122,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,
@@ -141,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[] = {
@@ -161,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,
@@ -195,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;
@@ -219,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;
} }
@@ -230,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);
} }
@@ -239,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;
@@ -257,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);
} }
@@ -274,7 +293,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 +330,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 +356,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 +386,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 +421,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 ********************************************************/
@@ -492,7 +553,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
@@ -562,7 +624,8 @@ static fx_iterator *iterator_begin(fx_object *obj)
static const fx_iterator *iterator_cbegin(const fx_object *obj) 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 +681,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);
@@ -648,42 +712,42 @@ static const fx_iterator_value iterator_get_cvalue(const fx_iterator *obj)
// ---- fx_directory DEFINITION // ---- fx_directory DEFINITION
FX_TYPE_CLASS_DEFINITION_BEGIN(fx_directory) FX_TYPE_CLASS_DEFINITION_BEGIN(fx_directory)
FX_TYPE_CLASS_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_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_iterable, FX_TYPE_ITERABLE) FX_TYPE_CLASS_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_INTERFACE_ENTRY(it_cbegin) = iterator_cbegin;
FX_TYPE_CLASS_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE) FX_TYPE_CLASS_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE)
FX_TYPE_CLASS_DEFINITION_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_DEFINITION_BEGIN(fx_directory_iterator)
FX_TYPE_CLASS_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_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_iterator, FX_TYPE_ITERATOR) FX_TYPE_CLASS_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_INTERFACE_ENTRY(it_get_cvalue) = iterator_get_cvalue;
FX_TYPE_CLASS_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR) FX_TYPE_CLASS_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR)
FX_TYPE_CLASS_DEFINITION_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)
+100 -29
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 ********************************************************/
+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)
{ {
+29 -12
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)
+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
+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)
+1 -3
View File
@@ -1,3 +1 @@
include(../cmake/Templates.cmake) export_fx_namespace_details(fx.serial)
add_fx_module(NAME serial DEPENDENCIES core ds)
+1 -1
View File
@@ -1,7 +1,7 @@
#ifndef FX_SERIAL_BITCODE_H_ #ifndef FX_SERIAL_BITCODE_H_
#define FX_SERIAL_BITCODE_H_ #define FX_SERIAL_BITCODE_H_
#include <fx/core/macros.h> #include <fx/macros.h>
FX_DECLS_BEGIN; FX_DECLS_BEGIN;
+5 -5
View File
@@ -1,11 +1,11 @@
#ifndef FX_SERIAL_CTX_H_ #ifndef FX_SERIAL_CTX_H_
#define FX_SERIAL_CTX_H_ #define FX_SERIAL_CTX_H_
#include <fx/core/macros.h> #include <fx/macros.h>
#include <fx/core/misc.h> #include <fx/misc.h>
#include <fx/core/object.h> #include <fx/object.h>
#include <fx/core/status.h> #include <fx/status.h>
#include <fx/core/stream.h> #include <fx/stream.h>
FX_DECLS_BEGIN; FX_DECLS_BEGIN;
+1 -1
View File
@@ -1,7 +1,7 @@
#ifndef FX_SERIAL_TOML_H_ #ifndef FX_SERIAL_TOML_H_
#define FX_SERIAL_TOML_H_ #define FX_SERIAL_TOML_H_
#include <fx/core/macros.h> #include <fx/macros.h>
FX_DECLS_BEGIN; FX_DECLS_BEGIN;
+21 -21
View File
@@ -1,14 +1,14 @@
#include <fx/core/error.h> #include <fx/collections/array.h>
#include <fx/core/status.h> #include <fx/collections/datetime.h>
#include <fx/core/stringstream.h> #include <fx/collections/dict.h>
#include <fx/ds/array.h> #include <fx/collections/hashmap.h>
#include <fx/ds/datetime.h> #include <fx/collections/number.h>
#include <fx/ds/dict.h> #include <fx/error.h>
#include <fx/ds/hashmap.h>
#include <fx/ds/number.h>
#include <fx/ds/string.h>
#include <fx/serial/ctx.h> #include <fx/serial/ctx.h>
#include <fx/serial/toml.h> #include <fx/serial/toml.h>
#include <fx/status.h>
#include <fx/string.h>
#include <fx/stringstream.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@@ -1400,7 +1400,7 @@ static void read_newline(struct ctx *ctx)
} }
enqueue_token(ctx, TOK_NEWLINE); enqueue_token(ctx, TOK_NEWLINE);
ctx->ctx_result = FX_SUCCESS; ctx->ctx_result = FX_RESULT_SUCCESS;
} }
static void read_comment(struct ctx *ctx) static void read_comment(struct ctx *ctx)
@@ -1670,7 +1670,7 @@ static fx_result parse_timestamp(struct ctx *ctx, fx_object **result)
tok->tok_value.time = NULL; tok->tok_value.time = NULL;
*result = (dt); *result = (dt);
return FX_SUCCESS; return FX_RESULT_SUCCESS;
} }
static fx_result parse_string(struct ctx *ctx, fx_object **result) static fx_result parse_string(struct ctx *ctx, fx_object **result)
@@ -1682,7 +1682,7 @@ static fx_result parse_string(struct ctx *ctx, fx_object **result)
} }
*result = (str); *result = (str);
return FX_SUCCESS; return FX_RESULT_SUCCESS;
} }
static fx_result parse_int(struct ctx *ctx, fx_object **result) static fx_result parse_int(struct ctx *ctx, fx_object **result)
@@ -1708,7 +1708,7 @@ static fx_result parse_int(struct ctx *ctx, fx_object **result)
} }
*result = (val); *result = (val);
return FX_SUCCESS; return FX_RESULT_SUCCESS;
} }
static fx_result parse_float(struct ctx *ctx, fx_object **result) static fx_result parse_float(struct ctx *ctx, fx_object **result)
@@ -1734,7 +1734,7 @@ static fx_result parse_float(struct ctx *ctx, fx_object **result)
} }
*result = (val); *result = (val);
return FX_SUCCESS; return FX_RESULT_SUCCESS;
} }
static fx_result parse_bool(struct ctx *ctx, fx_object **result) static fx_result parse_bool(struct ctx *ctx, fx_object **result)
@@ -1746,7 +1746,7 @@ static fx_result parse_bool(struct ctx *ctx, fx_object **result)
} }
*result = (val); *result = (val);
return FX_SUCCESS; return FX_RESULT_SUCCESS;
} }
static fx_result parse_table_inline(struct ctx *ctx, fx_object **result) static fx_result parse_table_inline(struct ctx *ctx, fx_object **result)
@@ -1763,7 +1763,7 @@ static fx_result parse_table_inline(struct ctx *ctx, fx_object **result)
struct token *tok = peek_token(ctx); struct token *tok = peek_token(ctx);
if (tok && tok->tok_type == TOK_RIGHT_BRACE) { if (tok && tok->tok_type == TOK_RIGHT_BRACE) {
*result = (table); *result = (table);
return FX_SUCCESS; return FX_RESULT_SUCCESS;
} }
bool done = false; bool done = false;
@@ -1797,7 +1797,7 @@ static fx_result parse_table_inline(struct ctx *ctx, fx_object **result)
} }
*result = (table); *result = (table);
return FX_SUCCESS; return FX_RESULT_SUCCESS;
} }
static void skip_newlines(struct ctx *ctx) static void skip_newlines(struct ctx *ctx)
@@ -1878,7 +1878,7 @@ static fx_result parse_array_inline(struct ctx *ctx, fx_object **result)
DISABLE_EXTENDED_LEXING(ctx); DISABLE_EXTENDED_LEXING(ctx);
*result = (array); *result = (array);
return FX_SUCCESS; return FX_RESULT_SUCCESS;
} }
static fx_result parse_value(struct ctx *ctx, fx_object **result) static fx_result parse_value(struct ctx *ctx, fx_object **result)
@@ -2005,7 +2005,7 @@ static fx_result parse_key_value_pair(struct ctx *ctx, fx_dict *container)
ctx_set_object_flags(ctx, value, OBJECT_KV_END_DEFINED); ctx_set_object_flags(ctx, value, OBJECT_KV_END_DEFINED);
} }
return FX_SUCCESS; return FX_RESULT_SUCCESS;
} }
static fx_result parse_table_header( static fx_result parse_table_header(
@@ -2101,7 +2101,7 @@ static fx_result parse_table_header(
advance_token(ctx); advance_token(ctx);
*new_container = new_table; *new_container = new_table;
return FX_SUCCESS; return FX_RESULT_SUCCESS;
} }
static fx_result parse_array_header( static fx_result parse_array_header(
@@ -2183,7 +2183,7 @@ static fx_result parse_array_header(
advance_token(ctx); advance_token(ctx);
*new_container = new_table; *new_container = new_table;
return FX_SUCCESS; return FX_RESULT_SUCCESS;
} }
static fx_result parse_root(struct ctx *ctx, fx_dict **out) static fx_result parse_root(struct ctx *ctx, fx_dict **out)
+1 -3
View File
@@ -1,3 +1 @@
include(../cmake/Templates.cmake) export_fx_namespace_details(fx.term)
add_fx_module(NAME term DEPENDENCIES core ds)
+49 -23
View File
@@ -1,11 +1,13 @@
#include <fx/core/error.h> #include <fx/error.h>
#include <fx/core/stringstream.h> #include <fx/stringstream.h>
#include <fx/term/tty.h> #include <fx/term/tty.h>
#include <inttypes.h> #include <inttypes.h>
static void get_error_id( static void get_error_id(
const struct fx_error_vendor *vendor, const struct fx_error *error, const struct fx_error_vendor *vendor,
char *out, size_t max) const struct fx_error *error,
char *out,
size_t max)
{ {
const char *vendor_name = NULL; const char *vendor_name = NULL;
const char *error_name = NULL; const char *error_name = NULL;
@@ -38,7 +40,8 @@ static void get_error_id(
} }
static void print_template_parameter( static void print_template_parameter(
const struct fx_error_template_parameter *params, size_t nr_params, const struct fx_error_template_parameter *params,
size_t nr_params,
const char *param_name) const char *param_name)
{ {
const struct fx_error_template_parameter *param = NULL; const struct fx_error_template_parameter *param = NULL;
@@ -122,7 +125,8 @@ static void print_template_parameter(
} }
static void print_content( static void print_content(
const struct fx_error_template_parameter *params, size_t nr_params, const struct fx_error_template_parameter *params,
size_t nr_params,
const char *s) const char *s)
{ {
if (!s) { if (!s) {
@@ -214,13 +218,16 @@ static void print_content(
static void print_submsg(const struct fx_error *error) static void print_submsg(const struct fx_error *error)
{ {
const struct fx_error_definition *error_def = fx_error_get_definition(error); const struct fx_error_definition *error_def
= fx_error_get_definition(error);
const struct fx_error_submsg *submsg = fx_error_get_first_submsg(error); const struct fx_error_submsg *submsg = fx_error_get_first_submsg(error);
while (submsg) { while (submsg) {
enum fx_error_submsg_type type = fx_error_submsg_get_type(submsg); enum fx_error_submsg_type type
= fx_error_submsg_get_type(submsg);
const char *content = fx_error_submsg_get_content(submsg); const char *content = fx_error_submsg_get_content(submsg);
const struct fx_error_msg *msg = fx_error_submsg_get_msg(submsg); const struct fx_error_msg *msg
= fx_error_submsg_get_msg(submsg);
const struct fx_error_template_parameter *params const struct fx_error_template_parameter *params
= fx_error_submsg_get_template_parameters(submsg); = fx_error_submsg_get_template_parameters(submsg);
@@ -229,16 +236,21 @@ static void print_submsg(const struct fx_error *error)
switch (type) { switch (type) {
case FX_ERROR_SUBMSG_ERROR: case FX_ERROR_SUBMSG_ERROR:
fx_tty_printf( fx_tty_printf(
fx_stdtty_err, 0, "[bright_red,bold]>[reset] "); fx_stdtty_err,
0,
"[bright_red,bold]>[reset] ");
break; break;
case FX_ERROR_SUBMSG_WARNING: case FX_ERROR_SUBMSG_WARNING:
fx_tty_printf( fx_tty_printf(
fx_stdtty_err, 0, fx_stdtty_err,
0,
"[bright_yellow,bold]>[reset] "); "[bright_yellow,bold]>[reset] ");
break; break;
case FX_ERROR_SUBMSG_INFO: case FX_ERROR_SUBMSG_INFO:
fx_tty_printf( fx_tty_printf(
fx_stdtty_err, 0, "[bright_cyan,bold]>[reset] "); fx_stdtty_err,
0,
"[bright_cyan,bold]>[reset] ");
break; break;
default: default:
break; break;
@@ -246,11 +258,14 @@ static void print_submsg(const struct fx_error *error)
if (msg) { if (msg) {
print_content( print_content(
params, FX_ERROR_TEMPLATE_PARAMETER_MAX, params,
FX_ERROR_TEMPLATE_PARAMETER_MAX,
fx_error_msg_get_content(msg)); fx_error_msg_get_content(msg));
} else if (content) { } else if (content) {
print_content( print_content(
params, FX_ERROR_TEMPLATE_PARAMETER_MAX, content); params,
FX_ERROR_TEMPLATE_PARAMETER_MAX,
content);
} }
fx_tty_printf(fx_stdtty_err, 0, "\n"); fx_tty_printf(fx_stdtty_err, 0, "\n");
@@ -293,8 +308,11 @@ static void print_stack_trace(const struct fx_error *error)
file = get_short_filepath(file); file = get_short_filepath(file);
fx_tty_printf( fx_tty_printf(
fx_stdtty_err, 0, fx_stdtty_err,
" [dark_grey]at %s() (%s:%u)[reset]\n", func, file, 0,
" [dark_grey]at %s() (%s:%u)[reset]\n",
func,
file,
line); line);
frame = fx_error_get_next_stack_frame(error, frame); frame = fx_error_get_next_stack_frame(error, frame);
@@ -302,14 +320,17 @@ static void print_stack_trace(const struct fx_error *error)
} }
static void report_error( static void report_error(
const fx_error *error, fx_error_report_flags flags, bool caused_by) const fx_error *error,
fx_error_report_flags flags,
bool caused_by)
{ {
const fx_error_vendor *vendor = fx_error_get_vendor(error); const fx_error_vendor *vendor = fx_error_get_vendor(error);
char error_id[128]; char error_id[128];
get_error_id(vendor, error, error_id, sizeof error_id); get_error_id(vendor, error, error_id, sizeof error_id);
const struct fx_error_definition *error_def = fx_error_get_definition(error); const struct fx_error_definition *error_def
= fx_error_get_definition(error);
fx_error_status_code code = fx_error_get_status_code(error); fx_error_status_code code = fx_error_get_status_code(error);
const char *description = fx_error_get_description(error); const char *description = fx_error_get_description(error);
const struct fx_error_template_parameter *params const struct fx_error_template_parameter *params
@@ -317,12 +338,14 @@ static void report_error(
if (!description && vendor) { if (!description && vendor) {
description = fx_error_vendor_get_status_code_description( description = fx_error_vendor_get_status_code_description(
vendor, code); vendor,
code);
} }
if (caused_by) { if (caused_by) {
fx_tty_printf( fx_tty_printf(
fx_stdtty_err, 0, fx_stdtty_err,
0,
" [green]->[reset] caused by [bright_red,bold]ERROR "); " [green]->[reset] caused by [bright_red,bold]ERROR ");
} else { } else {
fx_tty_printf(fx_stdtty_err, 0, "[bright_red,bold]==> ERROR "); fx_tty_printf(fx_stdtty_err, 0, "[bright_red,bold]==> ERROR ");
@@ -339,12 +362,14 @@ static void report_error(
if (msg) { if (msg) {
fx_tty_printf(fx_stdtty_err, 0, ": "); fx_tty_printf(fx_stdtty_err, 0, ": ");
print_content( print_content(
params, FX_ERROR_TEMPLATE_PARAMETER_MAX, params,
FX_ERROR_TEMPLATE_PARAMETER_MAX,
fx_error_msg_get_content(msg)); fx_error_msg_get_content(msg));
} else if (description) { } else if (description) {
fx_tty_printf(fx_stdtty_err, 0, ": "); fx_tty_printf(fx_stdtty_err, 0, ": ");
print_content( print_content(
params, FX_ERROR_TEMPLATE_PARAMETER_MAX, params,
FX_ERROR_TEMPLATE_PARAMETER_MAX,
description); description);
} }
} }
@@ -366,7 +391,8 @@ static void report_error(
} }
void fx_enhanced_error_reporter( void fx_enhanced_error_reporter(
const struct fx_error *error, fx_error_report_flags flags) const struct fx_error *error,
fx_error_report_flags flags)
{ {
report_error(error, flags, false); report_error(error, flags, false);
} }
-10
View File
@@ -1,10 +0,0 @@
#ifndef FX_TERM_H_
#define FX_TERM_H_
#include <fx/core/status.h>
#include <stdio.h>
#include <fx/term/tty.h>
#include <fx/term/print.h>
#endif
+7 -4
View File
@@ -1,8 +1,8 @@
#ifndef FX_TERM_PRINT_H_ #ifndef FX_TERM_PRINT_H_
#define FX_TERM_PRINT_H_ #define FX_TERM_PRINT_H_
#include <fx/core/misc.h> #include <fx/misc.h>
#include <fx/core/status.h> #include <fx/status.h>
#include <fx/term/tty.h> #include <fx/term/tty.h>
#include <stdarg.h> #include <stdarg.h>
@@ -40,13 +40,16 @@ typedef enum fx_print_format {
FX_API fx_status fx_print(fx_print_format format, const char *str, ...); FX_API fx_status fx_print(fx_print_format format, const char *str, ...);
FX_API fx_status fx_print_paragraph( FX_API fx_status fx_print_paragraph(
const char *str, struct fx_tty *tty, fx_paragraph_format *format); const char *str,
struct fx_tty *tty,
fx_paragraph_format *format);
FX_API int fx_putc(char c); FX_API int fx_putc(char c);
FX_API int fx_puts(const char *s); FX_API int fx_puts(const char *s);
FX_API int fx_printf(const char *format, ...); FX_API int fx_printf(const char *format, ...);
FX_API void fx_enhanced_error_reporter( FX_API void fx_enhanced_error_reporter(
const struct fx_error *, enum fx_error_report_flags flags); const struct fx_error *,
enum fx_error_report_flags flags);
#endif #endif
+39 -22
View File
@@ -1,38 +1,35 @@
#ifndef FX_TERM_TTY_H_ #ifndef FX_TERM_TTY_H_
#define FX_TERM_TTY_H_ #define FX_TERM_TTY_H_
#include <fx/core/misc.h> #include <fx/misc.h>
#include <fx/core/status.h> #include <fx/status.h>
#include <stdarg.h> #include <stdarg.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#define fx_stdtty (z__fx_tty_get_std()) #define fx_stdtty (z__fx_tty_get_std())
#define fx_stdtty_err (z__fx_tty_get_err()) #define fx_stdtty_err (z__fx_tty_get_err())
#define FX_TTY_CTRL_KEY(c) ((c) | FX_MOD_CTRL) #define FX_TTY_CTRL_KEY(c) ((c) | FX_MOD_CTRL)
#define FX_MAKE_VMODE(fg, bg, a) \ #define FX_MAKE_VMODE(fg, bg, a) {.v_fg = fg, .v_bg = bg, .v_attrib = (a)}
{ \
.v_fg = fg, .v_bg = bg, .v_attrib = (a) \
}
#define FX_MAKE_COLOUR_DEFAULT(v) \ #define FX_MAKE_COLOUR_DEFAULT(v) \
{ \ { \
.c_mode = TTY_COLOUR_NONE, \ .c_mode = TTY_COLOUR_NONE, \
} }
#define FX_MAKE_COLOUR_16(v) \ #define FX_MAKE_COLOUR_16(v) \
{ \ { \
.c_mode = TTY_COLOUR_16, .c_16 = {.value = (v) } \ .c_mode = TTY_COLOUR_16, .c_16 = {.value = (v) } \
} }
#define FX_MAKE_COLOUR_256(v) \ #define FX_MAKE_COLOUR_256(v) \
{ \ { \
.c_mode = TTY_COLOUR_256, .c_256 = {.value = (v) } \ .c_mode = TTY_COLOUR_256, .c_256 = {.value = (v) } \
} }
#define FX_MAKE_COLOUR_TRUE(cr, cg, cb) \ #define FX_MAKE_COLOUR_TRUE(cr, cg, cb) \
{ \ { \
.c_mode = TTY_COLOUR_TRUE, .c_true \ .c_mode = TTY_COLOUR_TRUE, .c_true \
= {.r = (cr), \ = {.r = (cr), \
@@ -152,29 +149,49 @@ static inline unsigned int fx_keycode_get_key(fx_keycode v)
FX_API bool fx_tty_is_interactive(const struct fx_tty *tty); FX_API bool fx_tty_is_interactive(const struct fx_tty *tty);
FX_API void fx_tty_set_mode(struct fx_tty *tty, enum fx_tty_mode mode); FX_API void fx_tty_set_mode(struct fx_tty *tty, enum fx_tty_mode mode);
FX_API void fx_tty_set_vmode(struct fx_tty *tty, const struct fx_tty_vmode *vmode); FX_API void fx_tty_set_vmode(
struct fx_tty *tty,
const struct fx_tty_vmode *vmode);
FX_API void fx_tty_reset_vmode(struct fx_tty *tty); FX_API void fx_tty_reset_vmode(struct fx_tty *tty);
FX_API enum fx_status fx_tty_get_dimensions( FX_API enum fx_status fx_tty_get_dimensions(
struct fx_tty *tty, unsigned int *w, unsigned int *h); struct fx_tty *tty,
unsigned int *w,
unsigned int *h);
FX_API enum fx_status fx_tty_get_cursor_position( FX_API enum fx_status fx_tty_get_cursor_position(
struct fx_tty *tty, unsigned int *x, unsigned int *y); struct fx_tty *tty,
unsigned int *x,
unsigned int *y);
FX_API fx_keycode fx_tty_read_key(struct fx_tty *tty); FX_API fx_keycode fx_tty_read_key(struct fx_tty *tty);
FX_API void fx_tty_move_cursor_x( FX_API void fx_tty_move_cursor_x(
struct fx_tty *tty, enum fx_tty_position_base base, int pos); struct fx_tty *tty,
enum fx_tty_position_base base,
int pos);
FX_API void fx_tty_move_cursor_y( FX_API void fx_tty_move_cursor_y(
struct fx_tty *tty, enum fx_tty_position_base base, int pos); struct fx_tty *tty,
enum fx_tty_position_base base,
int pos);
FX_API void fx_tty_clear(struct fx_tty *tty, enum fx_tty_clear_mode mode); FX_API void fx_tty_clear(struct fx_tty *tty, enum fx_tty_clear_mode mode);
FX_API int fx_tty_putc(struct fx_tty *tty, enum fx_tty_print_flags flags, char c); FX_API int fx_tty_putc(
struct fx_tty *tty,
enum fx_tty_print_flags flags,
char c);
FX_API int fx_tty_puts( FX_API int fx_tty_puts(
struct fx_tty *tty, enum fx_tty_print_flags flags, const char *s); struct fx_tty *tty,
enum fx_tty_print_flags flags,
const char *s);
FX_API int fx_tty_printf( FX_API int fx_tty_printf(
struct fx_tty *tty, enum fx_tty_print_flags flags, const char *s, ...); struct fx_tty *tty,
enum fx_tty_print_flags flags,
const char *s,
...);
FX_API int fx_tty_vprintf( FX_API int fx_tty_vprintf(
struct fx_tty *tty, enum fx_tty_print_flags flags, const char *s, struct fx_tty *tty,
enum fx_tty_print_flags flags,
const char *s,
va_list args); va_list args);
#endif #endif
+19 -8
View File
@@ -1,6 +1,6 @@
#include <fx/ds/string.h>
#include <fx/term/print.h>
#include <ctype.h> #include <ctype.h>
#include <fx/string.h>
#include <fx/term/print.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@@ -9,7 +9,9 @@
#define DEFAULT_PARAGRAPH_WIDTH 160 #define DEFAULT_PARAGRAPH_WIDTH 160
static void indent( static void indent(
struct fx_paragraph_format *format, struct fx_tty *tty, unsigned int margin) struct fx_paragraph_format *format,
struct fx_tty *tty,
unsigned int margin)
{ {
unsigned int x = 0; unsigned int x = 0;
while (x < margin) { while (x < margin) {
@@ -19,7 +21,9 @@ static void indent(
} }
static unsigned int extract_line( static unsigned int extract_line(
const char **sp, unsigned int line_length, fx_string *out, const char **sp,
unsigned int line_length,
fx_string *out,
struct fx_paragraph_format *format) struct fx_paragraph_format *format)
{ {
const char *start = *sp; const char *start = *sp;
@@ -95,7 +99,9 @@ static unsigned int extract_line(
} }
static fx_status print_paragraph_tty( static fx_status print_paragraph_tty(
const char *str, struct fx_tty *tty, struct fx_paragraph_format *format) const char *str,
struct fx_tty *tty,
struct fx_paragraph_format *format)
{ {
unsigned int w = 0, h = 0; unsigned int w = 0, h = 0;
fx_tty_get_dimensions(tty, &w, &h); fx_tty_get_dimensions(tty, &w, &h);
@@ -134,7 +140,8 @@ static fx_status print_paragraph_tty(
need_indent = true; need_indent = true;
while (*str == '\n') { while (*str == '\n') {
if (format->p_flags & FX_PARAGRAPH_MULTI_LINE_BREAK) { if (format->p_flags
& FX_PARAGRAPH_MULTI_LINE_BREAK) {
fx_tty_putc(tty, 0, '\n'); fx_tty_putc(tty, 0, '\n');
} }
@@ -164,13 +171,17 @@ static fx_status print_paragraph_tty(
} }
static fx_status print_paragraph_file( static fx_status print_paragraph_file(
const char *str, FILE *fp, struct fx_paragraph_format *format) const char *str,
FILE *fp,
struct fx_paragraph_format *format)
{ {
return FX_SUCCESS; return FX_SUCCESS;
} }
fx_status fx_print_paragraph( fx_status fx_print_paragraph(
const char *str, struct fx_tty *fp, struct fx_paragraph_format *format) const char *str,
struct fx_tty *fp,
struct fx_paragraph_format *format)
{ {
return print_paragraph_tty(str, fp, format); return print_paragraph_tty(str, fp, format);
} }
+9 -6
View File
@@ -1,8 +1,8 @@
#include "print.h" #include "print.h"
#include <fx/core/hash.h>
#include <fx/term/print.h>
#include <ctype.h> #include <ctype.h>
#include <fx/hash.h>
#include <fx/term/print.h>
#include <stdarg.h> #include <stdarg.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h> #include <stdio.h>
@@ -131,17 +131,20 @@ int fx_putc(char c)
return fx_tty_putc(fx_stdtty, 0, c); return fx_tty_putc(fx_stdtty, 0, c);
} }
int fx_puts(const char* s) int fx_puts(const char *s)
{ {
return fx_tty_puts(fx_stdtty, 0, s); return fx_tty_puts(fx_stdtty, 0, s);
} }
int fx_printf(const char* format, ...) int fx_printf(const char *format, ...)
{ {
va_list arg; va_list arg;
va_start(arg, format); va_start(arg, format);
int x = fx_tty_vprintf( int x = fx_tty_vprintf(
fx_stdtty, FX_TTY_DISABLE_INTERPOLATED_FORMATTING, format, arg); fx_stdtty,
FX_TTY_DISABLE_INTERPOLATED_FORMATTING,
format,
arg);
va_end(arg); va_end(arg);
return x; return x;
} }
+13 -11
View File
@@ -1,8 +1,7 @@
#ifndef _FX_PRINT_H_ #ifndef _FX_PRINT_H_
#define _FX_PRINT_H_ #define _FX_PRINT_H_
#include <fx/core/misc.h> #include <fx/misc.h>
#include <fx/term.h>
#include <stdio.h> #include <stdio.h>
enum z__fx_stream_modifier { enum z__fx_stream_modifier {
@@ -26,28 +25,31 @@ enum z__fx_stream_modifier {
Z__FX_STREAM_MOD_RESET = 0x4000, Z__FX_STREAM_MOD_RESET = 0x4000,
}; };
#define Z__FX_STREAM_MOD_GET_FG_COLOUR(x) \ #define Z__FX_STREAM_MOD_GET_FG_COLOUR(x) \
((x) \ ((x) \
& (Z__FX_STREAM_MOD_BLACK | Z__FX_STREAM_MOD_RED \ & (Z__FX_STREAM_MOD_BLACK | Z__FX_STREAM_MOD_RED \
| Z__FX_STREAM_MOD_GREEN | Z__FX_STREAM_MOD_FX)) | Z__FX_STREAM_MOD_GREEN | Z__FX_STREAM_MOD_FX))
#define Z__FX_STREAM_MOD_CLEAR_FG_COLOUR(x) \ #define Z__FX_STREAM_MOD_CLEAR_FG_COLOUR(x) \
((x) \ ((x) \
& ~(Z__FX_STREAM_MOD_BLACK | Z__FX_STREAM_MOD_RED \ & ~(Z__FX_STREAM_MOD_BLACK | Z__FX_STREAM_MOD_RED \
| Z__FX_STREAM_MOD_GREEN | Z__FX_STREAM_MOD_FX)) | Z__FX_STREAM_MOD_GREEN | Z__FX_STREAM_MOD_FX))
#define Z__FX_STREAM_MOD_GET_BG_COLOUR(x) \ #define Z__FX_STREAM_MOD_GET_BG_COLOUR(x) \
((x) \ ((x) \
& (Z__FX_STREAM_MOD_BG_BLACK | Z__FX_STREAM_MOD_BG_RED \ & (Z__FX_STREAM_MOD_BG_BLACK | Z__FX_STREAM_MOD_BG_RED \
| Z__FX_STREAM_MOD_BG_GREEN | Z__FX_STREAM_MOD_BG_FX)) | Z__FX_STREAM_MOD_BG_GREEN | Z__FX_STREAM_MOD_BG_FX))
#define Z__FX_STREAM_MOD_CLEAR_BG_COLOUR(x) \ #define Z__FX_STREAM_MOD_CLEAR_BG_COLOUR(x) \
((x) \ ((x) \
& ~(Z__FX_STREAM_MOD_BG_BLACK | Z__FX_STREAM_MOD_BG_RED \ & ~(Z__FX_STREAM_MOD_BG_BLACK | Z__FX_STREAM_MOD_BG_RED \
| Z__FX_STREAM_MOD_BG_GREEN | Z__FX_STREAM_MOD_BG_FX)) | Z__FX_STREAM_MOD_BG_GREEN | Z__FX_STREAM_MOD_BG_FX))
FX_API int z__fx_stream_is_tty(FILE *fp); FX_API int z__fx_stream_is_tty(FILE *fp);
FX_API int z__fx_stream_dimensions(FILE *fp, unsigned int *w, unsigned int *h); FX_API int z__fx_stream_dimensions(FILE *fp, unsigned int *w, unsigned int *h);
FX_API int z__fx_stream_cursorpos( FX_API int z__fx_stream_cursorpos(
FILE *in, FILE *out, unsigned int *x, unsigned int *y); FILE *in,
FILE *out,
unsigned int *x,
unsigned int *y);
FX_API int z__fx_stream_set_modifier(FILE *fp, unsigned int mod); FX_API int z__fx_stream_set_modifier(FILE *fp, unsigned int mod);
#endif #endif
+101 -28
View File
@@ -1,6 +1,6 @@
#include "tty.h" #include "tty.h"
#include <fx/core/hash.h> #include <fx/hash.h>
#include <fx/term/tty.h> #include <fx/term/tty.h>
#include <string.h> #include <string.h>
@@ -8,7 +8,7 @@
#define MOD_HASH_RED 0x89e9be1960f4c21c #define MOD_HASH_RED 0x89e9be1960f4c21c
#define MOD_HASH_GREEN 0x0f40f029637fecbc #define MOD_HASH_GREEN 0x0f40f029637fecbc
#define MOD_HASH_YELLOW 0x8346a574925e75a9 #define MOD_HASH_YELLOW 0x8346a574925e75a9
#define MOD_HASH_FX 0xc5ccd29bc2dda64d #define MOD_HASH_FX 0xc5ccd29bc2dda64d
#define MOD_HASH_MAGENTA 0x6c90e772edbc8708 #define MOD_HASH_MAGENTA 0x6c90e772edbc8708
#define MOD_HASH_CYAN 0x70ae2e90c1bce27a #define MOD_HASH_CYAN 0x70ae2e90c1bce27a
#define MOD_HASH_WHITE 0xced973885856e206 #define MOD_HASH_WHITE 0xced973885856e206
@@ -17,7 +17,7 @@
#define MOD_HASH_BRIGHT_RED 0xbad8e3fe841b9385 #define MOD_HASH_BRIGHT_RED 0xbad8e3fe841b9385
#define MOD_HASH_BRIGHT_GREEN 0x11cc5e579bdd2fb9 #define MOD_HASH_BRIGHT_GREEN 0x11cc5e579bdd2fb9
#define MOD_HASH_BRIGHT_YELLOW 0xfd579007fe8579f6 #define MOD_HASH_BRIGHT_YELLOW 0xfd579007fe8579f6
#define MOD_HASH_BRIGHT_FX 0x57c76bf18badb6d6 #define MOD_HASH_BRIGHT_FX 0x57c76bf18badb6d6
#define MOD_HASH_BRIGHT_MAGENTA 0xf6ecc6d3fdfec129 #define MOD_HASH_BRIGHT_MAGENTA 0xf6ecc6d3fdfec129
#define MOD_HASH_BRIGHT_CYAN 0x03df73fd4e12ec6d #define MOD_HASH_BRIGHT_CYAN 0x03df73fd4e12ec6d
#define MOD_HASH_BRIGHT_WHITE 0xb5ebc3323f57d7fb #define MOD_HASH_BRIGHT_WHITE 0xb5ebc3323f57d7fb
@@ -26,7 +26,7 @@
#define MOD_HASH_BG_RED 0x145b1e4366c7d7aa #define MOD_HASH_BG_RED 0x145b1e4366c7d7aa
#define MOD_HASH_BG_GREEN 0xa00b8541d3b1e55a #define MOD_HASH_BG_GREEN 0xa00b8541d3b1e55a
#define MOD_HASH_BG_YELLOW 0x98b030fd86e3b3cf #define MOD_HASH_BG_YELLOW 0x98b030fd86e3b3cf
#define MOD_HASH_BG_FX 0xa15529109506b5df #define MOD_HASH_BG_FX 0xa15529109506b5df
#define MOD_HASH_BG_MAGENTA 0x86dbda99bcc86222 #define MOD_HASH_BG_MAGENTA 0x86dbda99bcc86222
#define MOD_HASH_BG_CYAN 0xf16a3104cf61a098 #define MOD_HASH_BG_CYAN 0xf16a3104cf61a098
#define MOD_HASH_BG_WHITE 0x3408c46ab5836674 #define MOD_HASH_BG_WHITE 0x3408c46ab5836674
@@ -35,7 +35,7 @@
#define MOD_HASH_BRIGHT_BG_RED 0x144f5dc138087701 #define MOD_HASH_BRIGHT_BG_RED 0x144f5dc138087701
#define MOD_HASH_BRIGHT_BG_GREEN 0xc4d88c6426ffe355 #define MOD_HASH_BRIGHT_BG_GREEN 0xc4d88c6426ffe355
#define MOD_HASH_BRIGHT_BG_YELLOW 0xf7bb000a4a792602 #define MOD_HASH_BRIGHT_BG_YELLOW 0xf7bb000a4a792602
#define MOD_HASH_BRIGHT_BG_FX 0x9b5c16d6807a1002 #define MOD_HASH_BRIGHT_BG_FX 0x9b5c16d6807a1002
#define MOD_HASH_BRIGHT_BG_MAGENTA 0xc59fb2196cdba3fd #define MOD_HASH_BRIGHT_BG_MAGENTA 0xc59fb2196cdba3fd
#define MOD_HASH_BRIGHT_BG_CYAN 0x46feb6dc999a6f09 #define MOD_HASH_BRIGHT_BG_CYAN 0x46feb6dc999a6f09
#define MOD_HASH_BRIGHT_BG_WHITE 0xa3e7d1da08826f5f #define MOD_HASH_BRIGHT_BG_WHITE 0xa3e7d1da08826f5f
@@ -93,53 +93,83 @@ static void apply_code_to_vmode(struct tty_format_buf *fmt)
fmt->vmode.v_fg.c_16.value = FX_TTY_COLOUR16_WHITE; fmt->vmode.v_fg.c_16.value = FX_TTY_COLOUR16_WHITE;
} }
if (COMPARE_MOD_NAME(modifier, "dark_grey", mod_hash, MOD_HASH_DARK_GREY)) { if (COMPARE_MOD_NAME(
modifier,
"dark_grey",
mod_hash,
MOD_HASH_DARK_GREY)) {
fmt->vmode.v_fg.c_mode = FX_TTY_COLOUR_16; fmt->vmode.v_fg.c_mode = FX_TTY_COLOUR_16;
fmt->vmode.v_fg.c_16.value = FX_TTY_COLOUR16_BRIGHT_BLACK; fmt->vmode.v_fg.c_16.value = FX_TTY_COLOUR16_BRIGHT_BLACK;
} }
if (COMPARE_MOD_NAME(modifier, "bright_red", mod_hash, MOD_HASH_BRIGHT_RED)) { if (COMPARE_MOD_NAME(
modifier,
"bright_red",
mod_hash,
MOD_HASH_BRIGHT_RED)) {
fmt->vmode.v_fg.c_mode = FX_TTY_COLOUR_16; fmt->vmode.v_fg.c_mode = FX_TTY_COLOUR_16;
fmt->vmode.v_fg.c_16.value = FX_TTY_COLOUR16_BRIGHT_RED; fmt->vmode.v_fg.c_16.value = FX_TTY_COLOUR16_BRIGHT_RED;
} }
if (COMPARE_MOD_NAME( if (COMPARE_MOD_NAME(
modifier, "bright_green", mod_hash, MOD_HASH_BRIGHT_GREEN)) { modifier,
"bright_green",
mod_hash,
MOD_HASH_BRIGHT_GREEN)) {
fmt->vmode.v_fg.c_mode = FX_TTY_COLOUR_16; fmt->vmode.v_fg.c_mode = FX_TTY_COLOUR_16;
fmt->vmode.v_fg.c_16.value = FX_TTY_COLOUR16_BRIGHT_GREEN; fmt->vmode.v_fg.c_16.value = FX_TTY_COLOUR16_BRIGHT_GREEN;
} }
if (COMPARE_MOD_NAME( if (COMPARE_MOD_NAME(
modifier, "bright_yellow", mod_hash, MOD_HASH_BRIGHT_YELLOW)) { modifier,
"bright_yellow",
mod_hash,
MOD_HASH_BRIGHT_YELLOW)) {
fmt->vmode.v_fg.c_mode = FX_TTY_COLOUR_16; fmt->vmode.v_fg.c_mode = FX_TTY_COLOUR_16;
fmt->vmode.v_fg.c_16.value = FX_TTY_COLOUR16_BRIGHT_YELLOW; fmt->vmode.v_fg.c_16.value = FX_TTY_COLOUR16_BRIGHT_YELLOW;
} }
if (COMPARE_MOD_NAME( if (COMPARE_MOD_NAME(
modifier, "bright_blue", mod_hash, MOD_HASH_BRIGHT_FX)) { modifier,
"bright_blue",
mod_hash,
MOD_HASH_BRIGHT_FX)) {
fmt->vmode.v_fg.c_mode = FX_TTY_COLOUR_16; fmt->vmode.v_fg.c_mode = FX_TTY_COLOUR_16;
fmt->vmode.v_fg.c_16.value = FX_TTY_COLOUR16_BRIGHT_FX; fmt->vmode.v_fg.c_16.value = FX_TTY_COLOUR16_BRIGHT_FX;
} }
if (COMPARE_MOD_NAME( if (COMPARE_MOD_NAME(
modifier, "bright_magenta", mod_hash, MOD_HASH_BRIGHT_MAGENTA)) { modifier,
"bright_magenta",
mod_hash,
MOD_HASH_BRIGHT_MAGENTA)) {
fmt->vmode.v_fg.c_mode = FX_TTY_COLOUR_16; fmt->vmode.v_fg.c_mode = FX_TTY_COLOUR_16;
fmt->vmode.v_fg.c_16.value = FX_TTY_COLOUR16_BRIGHT_MAGENTA; fmt->vmode.v_fg.c_16.value = FX_TTY_COLOUR16_BRIGHT_MAGENTA;
} }
if (COMPARE_MOD_NAME( if (COMPARE_MOD_NAME(
modifier, "bright_cyan", mod_hash, MOD_HASH_BRIGHT_CYAN)) { modifier,
"bright_cyan",
mod_hash,
MOD_HASH_BRIGHT_CYAN)) {
fmt->vmode.v_fg.c_mode = FX_TTY_COLOUR_16; fmt->vmode.v_fg.c_mode = FX_TTY_COLOUR_16;
fmt->vmode.v_fg.c_16.value = FX_TTY_COLOUR16_BRIGHT_CYAN; fmt->vmode.v_fg.c_16.value = FX_TTY_COLOUR16_BRIGHT_CYAN;
} }
if (COMPARE_MOD_NAME( if (COMPARE_MOD_NAME(
modifier, "bright_white", mod_hash, MOD_HASH_BRIGHT_WHITE)) { modifier,
"bright_white",
mod_hash,
MOD_HASH_BRIGHT_WHITE)) {
fmt->vmode.v_fg.c_mode = FX_TTY_COLOUR_16; fmt->vmode.v_fg.c_mode = FX_TTY_COLOUR_16;
fmt->vmode.v_fg.c_16.value = FX_TTY_COLOUR16_BRIGHT_WHITE; fmt->vmode.v_fg.c_16.value = FX_TTY_COLOUR16_BRIGHT_WHITE;
} }
if (COMPARE_MOD_NAME(modifier, "bg_black", mod_hash, MOD_HASH_BG_BLACK)) { if (COMPARE_MOD_NAME(
modifier,
"bg_black",
mod_hash,
MOD_HASH_BG_BLACK)) {
fmt->vmode.v_bg.c_mode = FX_TTY_COLOUR_16; fmt->vmode.v_bg.c_mode = FX_TTY_COLOUR_16;
fmt->vmode.v_bg.c_16.value = FX_TTY_COLOUR16_BLACK; fmt->vmode.v_bg.c_16.value = FX_TTY_COLOUR16_BLACK;
} }
@@ -149,12 +179,20 @@ static void apply_code_to_vmode(struct tty_format_buf *fmt)
fmt->vmode.v_bg.c_16.value = FX_TTY_COLOUR16_RED; fmt->vmode.v_bg.c_16.value = FX_TTY_COLOUR16_RED;
} }
if (COMPARE_MOD_NAME(modifier, "bg_green", mod_hash, MOD_HASH_BG_GREEN)) { if (COMPARE_MOD_NAME(
modifier,
"bg_green",
mod_hash,
MOD_HASH_BG_GREEN)) {
fmt->vmode.v_bg.c_mode = FX_TTY_COLOUR_16; fmt->vmode.v_bg.c_mode = FX_TTY_COLOUR_16;
fmt->vmode.v_bg.c_16.value = FX_TTY_COLOUR16_GREEN; fmt->vmode.v_bg.c_16.value = FX_TTY_COLOUR16_GREEN;
} }
if (COMPARE_MOD_NAME(modifier, "bg_yellow", mod_hash, MOD_HASH_BG_YELLOW)) { if (COMPARE_MOD_NAME(
modifier,
"bg_yellow",
mod_hash,
MOD_HASH_BG_YELLOW)) {
fmt->vmode.v_bg.c_mode = FX_TTY_COLOUR_16; fmt->vmode.v_bg.c_mode = FX_TTY_COLOUR_16;
fmt->vmode.v_bg.c_16.value = FX_TTY_COLOUR16_YELLOW; fmt->vmode.v_bg.c_16.value = FX_TTY_COLOUR16_YELLOW;
} }
@@ -164,7 +202,11 @@ static void apply_code_to_vmode(struct tty_format_buf *fmt)
fmt->vmode.v_bg.c_16.value = FX_TTY_COLOUR16_FX; fmt->vmode.v_bg.c_16.value = FX_TTY_COLOUR16_FX;
} }
if (COMPARE_MOD_NAME(modifier, "bg_magenta", mod_hash, MOD_HASH_BG_MAGENTA)) { if (COMPARE_MOD_NAME(
modifier,
"bg_magenta",
mod_hash,
MOD_HASH_BG_MAGENTA)) {
fmt->vmode.v_bg.c_mode = FX_TTY_COLOUR_16; fmt->vmode.v_bg.c_mode = FX_TTY_COLOUR_16;
fmt->vmode.v_bg.c_16.value = FX_TTY_COLOUR16_MAGENTA; fmt->vmode.v_bg.c_16.value = FX_TTY_COLOUR16_MAGENTA;
} }
@@ -174,58 +216,82 @@ static void apply_code_to_vmode(struct tty_format_buf *fmt)
fmt->vmode.v_bg.c_16.value = FX_TTY_COLOUR16_CYAN; fmt->vmode.v_bg.c_16.value = FX_TTY_COLOUR16_CYAN;
} }
if (COMPARE_MOD_NAME(modifier, "bg_white", mod_hash, MOD_HASH_BG_WHITE)) { if (COMPARE_MOD_NAME(
modifier,
"bg_white",
mod_hash,
MOD_HASH_BG_WHITE)) {
fmt->vmode.v_bg.c_mode = FX_TTY_COLOUR_16; fmt->vmode.v_bg.c_mode = FX_TTY_COLOUR_16;
fmt->vmode.v_bg.c_16.value = FX_TTY_COLOUR16_WHITE; fmt->vmode.v_bg.c_16.value = FX_TTY_COLOUR16_WHITE;
} }
if (COMPARE_MOD_NAME( if (COMPARE_MOD_NAME(
modifier, "bg_dark_grey", mod_hash, MOD_HASH_BG_DARK_GREY)) { modifier,
"bg_dark_grey",
mod_hash,
MOD_HASH_BG_DARK_GREY)) {
fmt->vmode.v_bg.c_mode = FX_TTY_COLOUR_16; fmt->vmode.v_bg.c_mode = FX_TTY_COLOUR_16;
fmt->vmode.v_bg.c_16.value = FX_TTY_COLOUR16_BRIGHT_BLACK; fmt->vmode.v_bg.c_16.value = FX_TTY_COLOUR16_BRIGHT_BLACK;
} }
if (COMPARE_MOD_NAME( if (COMPARE_MOD_NAME(
modifier, "bright_bg_red", mod_hash, MOD_HASH_BRIGHT_BG_RED)) { modifier,
"bright_bg_red",
mod_hash,
MOD_HASH_BRIGHT_BG_RED)) {
fmt->vmode.v_bg.c_mode = FX_TTY_COLOUR_16; fmt->vmode.v_bg.c_mode = FX_TTY_COLOUR_16;
fmt->vmode.v_bg.c_16.value = FX_TTY_COLOUR16_BRIGHT_RED; fmt->vmode.v_bg.c_16.value = FX_TTY_COLOUR16_BRIGHT_RED;
} }
if (COMPARE_MOD_NAME( if (COMPARE_MOD_NAME(
modifier, "bright_bg_green", mod_hash, modifier,
"bright_bg_green",
mod_hash,
MOD_HASH_BRIGHT_BG_GREEN)) { MOD_HASH_BRIGHT_BG_GREEN)) {
fmt->vmode.v_bg.c_mode = FX_TTY_COLOUR_16; fmt->vmode.v_bg.c_mode = FX_TTY_COLOUR_16;
fmt->vmode.v_bg.c_16.value = FX_TTY_COLOUR16_BRIGHT_GREEN; fmt->vmode.v_bg.c_16.value = FX_TTY_COLOUR16_BRIGHT_GREEN;
} }
if (COMPARE_MOD_NAME( if (COMPARE_MOD_NAME(
modifier, "bright_bg_yellow", mod_hash, modifier,
"bright_bg_yellow",
mod_hash,
MOD_HASH_BRIGHT_BG_YELLOW)) { MOD_HASH_BRIGHT_BG_YELLOW)) {
fmt->vmode.v_bg.c_mode = FX_TTY_COLOUR_16; fmt->vmode.v_bg.c_mode = FX_TTY_COLOUR_16;
fmt->vmode.v_bg.c_16.value = FX_TTY_COLOUR16_BRIGHT_YELLOW; fmt->vmode.v_bg.c_16.value = FX_TTY_COLOUR16_BRIGHT_YELLOW;
} }
if (COMPARE_MOD_NAME( if (COMPARE_MOD_NAME(
modifier, "bright_bg_blue", mod_hash, MOD_HASH_BRIGHT_BG_FX)) { modifier,
"bright_bg_blue",
mod_hash,
MOD_HASH_BRIGHT_BG_FX)) {
fmt->vmode.v_bg.c_mode = FX_TTY_COLOUR_16; fmt->vmode.v_bg.c_mode = FX_TTY_COLOUR_16;
fmt->vmode.v_bg.c_16.value = FX_TTY_COLOUR16_BRIGHT_FX; fmt->vmode.v_bg.c_16.value = FX_TTY_COLOUR16_BRIGHT_FX;
} }
if (COMPARE_MOD_NAME( if (COMPARE_MOD_NAME(
modifier, "bright_bg_magenta", mod_hash, modifier,
"bright_bg_magenta",
mod_hash,
MOD_HASH_BRIGHT_BG_MAGENTA)) { MOD_HASH_BRIGHT_BG_MAGENTA)) {
fmt->vmode.v_bg.c_mode = FX_TTY_COLOUR_16; fmt->vmode.v_bg.c_mode = FX_TTY_COLOUR_16;
fmt->vmode.v_bg.c_16.value = FX_TTY_COLOUR16_BRIGHT_MAGENTA; fmt->vmode.v_bg.c_16.value = FX_TTY_COLOUR16_BRIGHT_MAGENTA;
} }
if (COMPARE_MOD_NAME( if (COMPARE_MOD_NAME(
modifier, "bright_bg_cyan", mod_hash, MOD_HASH_BRIGHT_BG_CYAN)) { modifier,
"bright_bg_cyan",
mod_hash,
MOD_HASH_BRIGHT_BG_CYAN)) {
fmt->vmode.v_bg.c_mode = FX_TTY_COLOUR_16; fmt->vmode.v_bg.c_mode = FX_TTY_COLOUR_16;
fmt->vmode.v_bg.c_16.value = FX_TTY_COLOUR16_BRIGHT_CYAN; fmt->vmode.v_bg.c_16.value = FX_TTY_COLOUR16_BRIGHT_CYAN;
} }
if (COMPARE_MOD_NAME( if (COMPARE_MOD_NAME(
modifier, "bright_bg_white", mod_hash, modifier,
"bright_bg_white",
mod_hash,
MOD_HASH_BRIGHT_BG_WHITE)) { MOD_HASH_BRIGHT_BG_WHITE)) {
fmt->vmode.v_bg.c_mode = FX_TTY_COLOUR_16; fmt->vmode.v_bg.c_mode = FX_TTY_COLOUR_16;
fmt->vmode.v_bg.c_16.value = FX_TTY_COLOUR16_BRIGHT_WHITE; fmt->vmode.v_bg.c_16.value = FX_TTY_COLOUR16_BRIGHT_WHITE;
@@ -328,7 +394,10 @@ int fx_tty_putc(struct fx_tty *tty, enum fx_tty_print_flags flags, char c)
return c; return c;
} }
int fx_tty_puts(struct fx_tty *tty, enum fx_tty_print_flags flags, const char *s) int fx_tty_puts(
struct fx_tty *tty,
enum fx_tty_print_flags flags,
const char *s)
{ {
int r = 0; int r = 0;
@@ -340,7 +409,11 @@ int fx_tty_puts(struct fx_tty *tty, enum fx_tty_print_flags flags, const char *s
return r; return r;
} }
int fx_tty_printf(struct fx_tty *tty, enum fx_tty_print_flags flags, const char *s, ...) int fx_tty_printf(
struct fx_tty *tty,
enum fx_tty_print_flags flags,
const char *s,
...)
{ {
va_list arg; va_list arg;
va_start(arg, s); va_start(arg, s);
+2 -7
View File
@@ -1,7 +1,2 @@
set(namespace_include_paths ${CMAKE_CURRENT_SOURCE_DIR}/include PARENT_SCOPE) set(source_dirs hash)
export_fx_namespace_details(fx)
file(GLOB namespace_sources
*.c *.h
hash/*.c hash/*.h
${CMAKE_CURRENT_SOURCE_DIR}/include/fx/*.h)
set(namespace_sources ${namespace_sources} PARENT_SCOPE)
+1 -1
View File
@@ -1,4 +1,4 @@
#include <fx/core/bitop.h> #include <fx/bitop.h>
int fx_popcountl(long v) int fx_popcountl(long v)
{ {
+2
View File
@@ -1,3 +1,4 @@
#if 0
#include <assert.h> #include <assert.h>
#include <fx/core/error.h> #include <fx/core/error.h>
#include <fx/core/thread.h> #include <fx/core/thread.h>
@@ -59,3 +60,4 @@ bool fx_mutex_unlock(fx_mutex *mut)
{ {
return pthread_mutex_unlock(mut) == 0; return pthread_mutex_unlock(mut) == 0;
} }
#endif