From b5c4d7aeccbf07e7dfe44d90bb8494a1a6eb30dd Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Wed, 29 Jun 2022 02:51:24 +0300 Subject: [PATCH] Remove cmdline file API (#105) --- .gitignore | 2 - ChangeLog | 5 --- Makefile.am | 3 +- README.md | 5 +-- examples/Makefile.am | 18 --------- examples/cmdline_file.c | 71 ----------------------------------- examples/generic_file.c | 2 +- examples/generic_mutex.c | 4 +- examples/memory_file.c | 16 -------- include/Makefile.am | 3 +- include/kernaux.h.in | 1 - include/kernaux/cmdline.h | 9 ----- include/kernaux/memory_file.h | 37 ------------------ src/cmdline.c | 28 -------------- src/memory_file.c | 51 ------------------------- tests/cmdline_test.c | 63 ------------------------------- 16 files changed, 6 insertions(+), 312 deletions(-) delete mode 100644 examples/cmdline_file.c delete mode 100644 examples/memory_file.c delete mode 100644 include/kernaux/memory_file.h delete mode 100644 src/memory_file.c diff --git a/.gitignore b/.gitignore index 932444ed..167f0e63 100644 --- a/.gitignore +++ b/.gitignore @@ -107,13 +107,11 @@ /examples/assert /examples/cmdline -/examples/cmdline_file /examples/generic_file /examples/generic_malloc /examples/generic_mutex /examples/macro_container_of /examples/memmap -/examples/memory_file /examples/ntoa /examples/panic /examples/pfa diff --git a/ChangeLog b/ChangeLog index fb19bc6c..9305dd34 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,8 +1,3 @@ -2022-06-27 Alex Kotov - - * include/kernaux/memory_file.h: Added - * include/kernaux/cmdline: Added function "kernaux_cmdline_file" - 2022-06-25 Alex Kotov * configure.ac: Removed package "io" diff --git a/Makefile.am b/Makefile.am index 8575219b..5f42b2be 100644 --- a/Makefile.am +++ b/Makefile.am @@ -33,8 +33,7 @@ libkernaux_la_SOURCES = \ src/libc.h \ src/generic/file.c \ src/generic/malloc.c \ - src/generic/mutex.c \ - src/memory_file.c + src/generic/mutex.c ######## # libc # diff --git a/README.md b/README.md index df3c10ef..1905158e 100644 --- a/README.md +++ b/README.md @@ -47,8 +47,6 @@ zero). Work-in-progress APIs can change at any time. * Architecture-specific code (*work in progress*) * [Declarations](/include/kernaux/arch/) * [Functions](/include/kernaux/asm/) - * [Memory file](/include/kernaux/memory_file.h) (*non-breaking since* **?.?.?**) - * [Example](/examples/memory_file.c) * Generic types * [File](/include/kernaux/generic/file.h) (*non-breaking since* **?.?.?**) * [Example](/examples/generic_file.c) @@ -59,8 +57,7 @@ zero). Work-in-progress APIs can change at any time. * Algorithms * [Free list memory allocator](/include/kernaux/free_list.h) (*non-breaking since* **?.?.?**) * [Simple command line parser](/include/kernaux/cmdline.h) (*non-breaking since* **0.2.0**) - * [Example: buffer](/examples/cmdline.c) - * [Example: file](/examples/cmdline_file.c) + * [Example](/examples/cmdline.c) * [Page Frame Allocator](/include/kernaux/pfa.h) (*work in progress*) * [Example](/examples/pfa.c) * Data formats diff --git a/examples/Makefile.am b/examples/Makefile.am index c011b7a2..a9ce7706 100644 --- a/examples/Makefile.am +++ b/examples/Makefile.am @@ -21,16 +21,6 @@ cmdline_LDADD = $(top_builddir)/libkernaux.la cmdline_SOURCES = main.c cmdline.c endif -################ -# cmdline_file # -################ - -if WITH_CMDLINE -TESTS += cmdline_file -cmdline_file_LDADD = $(top_builddir)/libkernaux.la -cmdline_file_SOURCES = main.c cmdline_file.c -endif - ################ # generic_file # ################ @@ -73,14 +63,6 @@ memmap_LDADD = $(top_builddir)/libkernaux.la memmap_SOURCES = main.c memmap.c endif -############### -# memory_file # -############### - -TESTS += memory_file -memory_file_LDADD = $(top_builddir)/libkernaux.la -memory_file_SOURCES = main.c memory_file.c - ######## # ntoa # ######## diff --git a/examples/cmdline_file.c b/examples/cmdline_file.c deleted file mode 100644 index ee9b6fb4..00000000 --- a/examples/cmdline_file.c +++ /dev/null @@ -1,71 +0,0 @@ -#include -#include - -#include -#include -#include - -#define ARG_COUNT_MAX 100 -#define BUFFER_SIZE 4096 - -static const char *const cmdline = "foo bar\\ baz \"car cdr\""; - -static void with_args_indices_array(); -static void without_args_indices_array(); - -void example_main() -{ - with_args_indices_array(); - without_args_indices_array(); -} - -void with_args_indices_array() -{ - char error_msg[KERNAUX_CMDLINE_ERROR_MSG_SIZE_MAX]; - size_t argc; - size_t argv[ARG_COUNT_MAX]; - char buffer[BUFFER_SIZE]; - - struct KernAux_MemoryFile memory_file = - KernAux_MemoryFile_create(buffer, BUFFER_SIZE, NULL); - - assert(kernaux_cmdline_file( - cmdline, - error_msg, - &argc, - &memory_file.file, - argv, - ARG_COUNT_MAX - )); - - assert(strcmp(error_msg, "") == 0); - assert(argc == 3); - assert(strcmp(&buffer[argv[0]], "foo") == 0); - assert(strcmp(&buffer[argv[1]], "bar baz") == 0); - assert(strcmp(&buffer[argv[2]], "car cdr") == 0); -} - -void without_args_indices_array() -{ - char error_msg[KERNAUX_CMDLINE_ERROR_MSG_SIZE_MAX]; - size_t argc; - char buffer[BUFFER_SIZE]; - - struct KernAux_MemoryFile memory_file = - KernAux_MemoryFile_create(buffer, BUFFER_SIZE, NULL); - - assert(kernaux_cmdline_file( - cmdline, - error_msg, - &argc, - &memory_file.file, - NULL, - 0 - )); - - assert(strcmp(error_msg, "") == 0); - assert(argc == 3); - assert(strcmp(&buffer[0], "foo") == 0); - assert(strcmp(&buffer[4], "bar baz") == 0); - assert(strcmp(&buffer[12], "car cdr") == 0); -} diff --git a/examples/generic_file.c b/examples/generic_file.c index 510639ec..0dedd82d 100644 --- a/examples/generic_file.c +++ b/examples/generic_file.c @@ -47,7 +47,7 @@ struct MyFile MyFile_create(char *const ptr, const size_t size) int MyFile_putc(void *const file, const unsigned char c) { - MyFile my_file = file; + const MyFile my_file = file; if (my_file->pos >= my_file->size) return KERNAUX_EOF; my_file->ptr[my_file->pos++] = c; return c; diff --git a/examples/generic_mutex.c b/examples/generic_mutex.c index 7da30441..af7199b4 100644 --- a/examples/generic_mutex.c +++ b/examples/generic_mutex.c @@ -44,13 +44,13 @@ struct MyMutex MyMutex_create() void MyMutex_lock(void *const mutex) { - MyMutex my_mutex = mutex; + const MyMutex my_mutex = mutex; pthread_mutex_lock(&my_mutex->pthread_mutex); } void MyMutex_unlock(void *const mutex) { - MyMutex my_mutex = mutex; + const MyMutex my_mutex = mutex; pthread_mutex_unlock(&my_mutex->pthread_mutex); } diff --git a/examples/memory_file.c b/examples/memory_file.c deleted file mode 100644 index 7fb542cf..00000000 --- a/examples/memory_file.c +++ /dev/null @@ -1,16 +0,0 @@ -#include - -#include -#include - -static char buffer[4096]; - -void example_main() -{ - struct KernAux_MemoryFile memory_file = - KernAux_MemoryFile_create(buffer, sizeof(buffer), NULL); - - KernAux_File_puts(&memory_file.file, "Hello, World!"); - - assert(strcmp(buffer, "Hello, World!") == 0); -} diff --git a/include/Makefile.am b/include/Makefile.am index 6d6d82bf..a264a460 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -11,8 +11,7 @@ nobase_include_HEADERS = \ kernaux/version.h \ kernaux/generic/file.h \ kernaux/generic/malloc.h \ - kernaux/generic/mutex.h \ - kernaux/memory_file.h + kernaux/generic/mutex.h ######## # ARCH # diff --git a/include/kernaux.h.in b/include/kernaux.h.in index 6e245acd..3ce50c20 100644 --- a/include/kernaux.h.in +++ b/include/kernaux.h.in @@ -6,7 +6,6 @@ #include #include #include -#include #include #include diff --git a/include/kernaux/cmdline.h b/include/kernaux/cmdline.h index 84735125..a44a2761 100644 --- a/include/kernaux/cmdline.h +++ b/include/kernaux/cmdline.h @@ -24,15 +24,6 @@ bool kernaux_cmdline( size_t buffer_size ); -bool kernaux_cmdline_file( - const char *cmdline, - char *error_msg, - size_t *argc, - KernAux_File file, - size_t *arg_idxs, - size_t arg_count_max -); - #ifdef __cplusplus } #endif diff --git a/include/kernaux/memory_file.h b/include/kernaux/memory_file.h deleted file mode 100644 index 9ec57471..00000000 --- a/include/kernaux/memory_file.h +++ /dev/null @@ -1,37 +0,0 @@ -#ifndef KERNAUX_INCLUDED_MEMORY_FILE -#define KERNAUX_INCLUDED_MEMORY_FILE - -#ifdef __cplusplus -extern "C" { -#endif - -#include -#include -#include - -#include - -typedef struct KernAux_MemoryFile { - struct KernAux_File file; - - KernAux_Mutex KERNAUX_PRIVATE_FIELD(mutex); - char *KERNAUX_PRIVATE_FIELD(ptr); - size_t KERNAUX_PRIVATE_FIELD(size); - size_t KERNAUX_PRIVATE_FIELD(pos); -} *KernAux_MemoryFile; - -struct KernAux_MemoryFile -KernAux_MemoryFile_create(void *ptr, size_t size, KernAux_Mutex mutex); - -void KernAux_MemoryFile_init( - KernAux_MemoryFile memory_file, - void *ptr, - size_t size, - KernAux_Mutex mutex -); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/src/cmdline.c b/src/cmdline.c index f87c3fee..2527e76f 100644 --- a/src/cmdline.c +++ b/src/cmdline.c @@ -81,34 +81,6 @@ bool kernaux_cmdline( ); } -bool kernaux_cmdline_file( - const char *const cmdline, - char *const error_msg, - size_t *const argc, - const KernAux_File file, - size_t *arg_idxs, - size_t arg_count_max -) { - KERNAUX_ASSERT(cmdline); - KERNAUX_ASSERT(error_msg); - KERNAUX_ASSERT(argc); - KERNAUX_ASSERT(file); - KERNAUX_ASSERT(arg_idxs == NULL || arg_count_max > 0); - - return kernaux_cmdline_common( - cmdline, - error_msg, - argc, - '\0', // arg_terminator - NULL, - NULL, - file, - arg_idxs, - arg_count_max, - 0 - ); -} - /********************************* * Implementation: main function * *********************************/ diff --git a/src/memory_file.c b/src/memory_file.c deleted file mode 100644 index a721dedb..00000000 --- a/src/memory_file.c +++ /dev/null @@ -1,51 +0,0 @@ -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif - -#include -#include -#include -#include - -static int KernAux_MemoryFile_putc(void *file, unsigned char c); - -struct KernAux_MemoryFile KernAux_MemoryFile_create( - void *const ptr, - const size_t size, - const KernAux_Mutex mutex -) { - struct KernAux_MemoryFile memory_file; - KernAux_MemoryFile_init(&memory_file, ptr, size, mutex); - return memory_file; -} - -void KernAux_MemoryFile_init( - const KernAux_MemoryFile memory_file, - void *const ptr, - const size_t size, - const KernAux_Mutex mutex -) { - KERNAUX_ASSERT(memory_file); - KERNAUX_ASSERT(ptr); - KERNAUX_ASSERT(size); - - memory_file->file.putc = KernAux_MemoryFile_putc; - memory_file->file.puts = NULL; - memory_file->file.write = NULL; - memory_file->mutex = mutex; - memory_file->ptr = ptr; - memory_file->size = size; - memory_file->pos = 0; -} - -int KernAux_MemoryFile_putc(void *const file, const unsigned char c) -{ - const KernAux_MemoryFile memory_file = file; - KERNAUX_ASSERT(memory_file); - KERNAUX_ASSERT(memory_file->ptr); - KERNAUX_ASSERT(memory_file->size); - - if (memory_file->pos >= memory_file->size) return KERNAUX_EOF; - memory_file->ptr[memory_file->pos++] = c; - return c; -} diff --git a/tests/cmdline_test.c b/tests/cmdline_test.c index 654cae75..050b0685 100644 --- a/tests/cmdline_test.c +++ b/tests/cmdline_test.c @@ -1,7 +1,6 @@ #include "cmdline_test.h" #include -#include #include #include @@ -65,68 +64,6 @@ void test( } } - { - memset(error_msg, 'x', KERNAUX_CMDLINE_ERROR_MSG_SIZE_MAX); - memset(argv, 0, sizeof(char*) * arg_count_max); - memset(arg_idxs, 0, sizeof(size_t) * arg_count_max); - memset(buffer, 'x', buffer_size); - - struct KernAux_MemoryFile memory_file = - KernAux_MemoryFile_create(buffer, buffer_size, NULL); - - assert( - kernaux_cmdline_file( - cmdline, - error_msg, - &argc, - &memory_file.file, - arg_idxs, - arg_count_max - ) == !!expected_result - ); - - assert(strcmp(error_msg, expected_error_msg) == 0); - assert(argc == expected_argc); - - if (expected_argv) { - for (size_t index = 0; index < argc; ++index) { - assert(strcmp(&buffer[arg_idxs[index]], expected_argv[index]) == 0); - } - } - } - - if (strcmp(expected_error_msg, "too many args") != 0) { - memset(error_msg, 'x', KERNAUX_CMDLINE_ERROR_MSG_SIZE_MAX); - memset(argv, 0, sizeof(char*) * arg_count_max); - memset(arg_idxs, 0, sizeof(size_t) * arg_count_max); - memset(buffer, 'x', buffer_size); - - struct KernAux_MemoryFile memory_file = - KernAux_MemoryFile_create(buffer, buffer_size, NULL); - - assert( - kernaux_cmdline_file( - cmdline, - error_msg, - &argc, - &memory_file.file, - NULL, - 0 - ) == !!expected_result - ); - - assert(strcmp(error_msg, expected_error_msg) == 0); - assert(argc == expected_argc); - - if (expected_argv) { - const char *arg = buffer; - for (size_t index = 0; index < argc; ++index) { - assert(strcmp(expected_argv[index], arg) == 0); - arg += strlen(arg) + 1; - } - } - } - free(error_msg); free(argv); free(arg_idxs);