mirror of
https://github.com/tailix/libkernaux.git
synced 2025-02-10 15:36:29 -05:00
Remove cmdline file API (#105)
This commit is contained in:
parent
0765cd04e6
commit
b5c4d7aecc
16 changed files with 6 additions and 312 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -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
|
||||
|
|
|
@ -1,8 +1,3 @@
|
|||
2022-06-27 Alex Kotov <kotovalexarian@gmail.com>
|
||||
|
||||
* include/kernaux/memory_file.h: Added
|
||||
* include/kernaux/cmdline: Added function "kernaux_cmdline_file"
|
||||
|
||||
2022-06-25 Alex Kotov <kotovalexarian@gmail.com>
|
||||
|
||||
* configure.ac: Removed package "io"
|
||||
|
|
|
@ -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 #
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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 #
|
||||
########
|
||||
|
|
|
@ -1,71 +0,0 @@
|
|||
#include <kernaux/cmdline.h>
|
||||
#include <kernaux/memory_file.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
|
||||
#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);
|
||||
}
|
|
@ -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;
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,16 +0,0 @@
|
|||
#include <kernaux/memory_file.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
|
||||
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);
|
||||
}
|
|
@ -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 #
|
||||
|
|
|
@ -6,7 +6,6 @@
|
|||
#include <kernaux/assert.h>
|
||||
#include <kernaux/macro.h>
|
||||
#include <kernaux/version.h>
|
||||
#include <kernaux/memory_file.h>
|
||||
|
||||
#include <kernaux/generic/file.h>
|
||||
#include <kernaux/generic/malloc.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
|
||||
|
|
|
@ -1,37 +0,0 @@
|
|||
#ifndef KERNAUX_INCLUDED_MEMORY_FILE
|
||||
#define KERNAUX_INCLUDED_MEMORY_FILE
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <kernaux/generic/file.h>
|
||||
#include <kernaux/generic/mutex.h>
|
||||
#include <kernaux/macro.h>
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
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
|
|
@ -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 *
|
||||
*********************************/
|
||||
|
|
|
@ -1,51 +0,0 @@
|
|||
#ifdef HAVE_CONFIG_H
|
||||
#include "config.h"
|
||||
#endif
|
||||
|
||||
#include <kernaux/assert.h>
|
||||
#include <kernaux/generic/file.h>
|
||||
#include <kernaux/generic/mutex.h>
|
||||
#include <kernaux/memory_file.h>
|
||||
|
||||
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;
|
||||
}
|
|
@ -1,7 +1,6 @@
|
|||
#include "cmdline_test.h"
|
||||
|
||||
#include <kernaux/cmdline.h>
|
||||
#include <kernaux/memory_file.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdlib.h>
|
||||
|
@ -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);
|
||||
|
|
Loading…
Add table
Reference in a new issue