1
0
Fork 0
mirror of https://github.com/tailix/libkernaux.git synced 2024-11-13 11:04:27 -05:00
libkernaux/examples/cmdline_file.c

72 lines
1.6 KiB
C
Raw Normal View History

2022-06-27 10:46:29 -04:00
#include <kernaux/cmdline.h>
#include <kernaux/memory_file.h>
#include <assert.h>
#include <stddef.h>
#include <string.h>
2022-06-27 12:30:47 -04:00
#define ARG_COUNT_MAX 100
#define BUFFER_SIZE 4096
2022-06-27 10:46:29 -04:00
static const char *const cmdline = "foo bar\\ baz \"car cdr\"";
2022-06-27 12:30:47 -04:00
static void with_args_indices_array();
static void without_args_indices_array();
2022-06-27 10:46:29 -04:00
void example_main()
2022-06-27 12:30:47 -04:00
{
with_args_indices_array();
without_args_indices_array();
}
void with_args_indices_array()
2022-06-27 10:46:29 -04:00
{
char error_msg[KERNAUX_CMDLINE_ERROR_MSG_SIZE_MAX];
size_t argc;
2022-06-27 12:30:47 -04:00
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];
2022-06-27 10:46:29 -04:00
struct KernAux_MemoryFile memory_file =
2022-06-27 12:30:47 -04:00
KernAux_MemoryFile_create(buffer, BUFFER_SIZE, NULL);
2022-06-27 10:46:29 -04:00
assert(kernaux_cmdline_file(
cmdline,
error_msg,
&argc,
2022-06-27 12:30:47 -04:00
&memory_file.file,
NULL,
0
2022-06-27 10:46:29 -04:00
));
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);
}