From 5c087ded1dec1743519a7331c70fbd879bc7895f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leonard=20Ikl=C3=A9?= Date: Sat, 12 Mar 2022 12:32:06 +0100 Subject: [PATCH] added indention size detection --- README.md | 2 +- src/cargs.c | 48 ++++++++++++++++++++++++++++++++++++++++------ test/option_test.c | 7 +------ 3 files changed, 44 insertions(+), 13 deletions(-) mode change 100755 => 100644 src/cargs.c diff --git a/README.md b/README.md index f165121..e1a4260 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ -[![Travis Build](https://img.shields.io/travis/com/likle/cargs/master.svg?label=Linux%20%26%20MacOS)](https://app.travis-ci.com/github/likle/cargs) +[![Travis Build](https://img.shields.io/travis/com/likle/cargs/master?label=Linux%2C%20macOS%20%26%20FreeBSD)](https://app.travis-ci.com/github/likle/cargs) [![Appveyor Build](https://img.shields.io/appveyor/ci/likle/cargs/master.svg?label=Windows)](https://ci.appveyor.com/project/likle/cargs) [![codecov](https://img.shields.io/codecov/c/github/likle/cargs/master.svg?label=Coverage)](https://codecov.io/gh/likle/cargs) [![Language Grade: C/C++](https://img.shields.io/lgtm/grade/cpp/g/likle/cargs.svg?label=Code%20Quality)](https://lgtm.com/projects/g/likle/cargs/context:cpp) diff --git a/src/cargs.c b/src/cargs.c old mode 100755 new mode 100644 index c5a92a5..2d7d945 --- a/src/cargs.c +++ b/src/cargs.c @@ -4,8 +4,11 @@ #include #include +#define CAG_OPTION_PRINT_DISTANCE 4 +#define CAG_OPTION_PRINT_MIN_INDENTION 20 + static void cag_option_print_value(const cag_option *option, - int *accessor_length, FILE *destination) + size_t *accessor_length, FILE *destination) { if (option->value_name != NULL) { *accessor_length += fprintf(destination, "=%s", option->value_name); @@ -13,7 +16,7 @@ static void cag_option_print_value(const cag_option *option, } static void cag_option_print_letters(const cag_option *option, bool *first, - int *accessor_length, FILE *destination) + size_t *accessor_length, FILE *destination) { const char *access_letter; access_letter = option->access_letters; @@ -31,7 +34,7 @@ static void cag_option_print_letters(const cag_option *option, bool *first, } static void cag_option_print_name(const cag_option *option, bool *first, - int *accessor_length, FILE *destination) + size_t *accessor_length, FILE *destination) { if (option->access_name != NULL) { if (*first) { @@ -42,13 +45,46 @@ static void cag_option_print_name(const cag_option *option, bool *first, } } +static size_t cag_option_get_print_indention(const cag_option *options, + size_t option_count) +{ + size_t option_index, indention, result; + const cag_option *option; + + result = CAG_OPTION_PRINT_MIN_INDENTION; + + for (option_index = 0; option_index < option_count; ++option_index) { + indention = CAG_OPTION_PRINT_DISTANCE; + option = &options[option_index]; + if (option->access_letters != NULL && *option->access_letters) { + indention += strlen(option->access_letters) * 4 - 2; + if (option->access_name != NULL) { + indention += strlen(option->access_name) + 4; + } + } else if (option->access_name != NULL) { + indention += strlen(option->access_name) + 2; + } + + if (option->value_name != NULL) { + indention += strlen(option->value_name) + 1; + } + + if (indention > result) { + result = indention; + } + } + + return result; +} + void cag_option_print(const cag_option *options, size_t option_count, FILE *destination) { - size_t option_index; + size_t option_index, indention, i, accessor_length; const cag_option *option; bool first; - int i, accessor_length; + + indention = cag_option_get_print_indention(options, option_count); for (option_index = 0; option_index < option_count; ++option_index) { option = &options[option_index]; @@ -61,7 +97,7 @@ void cag_option_print(const cag_option *options, size_t option_count, cag_option_print_name(option, &first, &accessor_length, destination); cag_option_print_value(option, &accessor_length, destination); - for (i = accessor_length; i < 20; ++i) { + for (i = accessor_length; i < indention; ++i) { fputs(" ", destination); } diff --git a/test/option_test.c b/test/option_test.c index de2c92e..c2dfff4 100755 --- a/test/option_test.c +++ b/test/option_test.c @@ -627,18 +627,15 @@ int option_print(void) { char buf[255]; const char *expected; - const char *test_file_name; FILE *test_file; - test_file_name = "option_print.out"; expected = " -s Simple flag\n" " -a Another simple flag\n" " -m, -M, -o, -O Multiple access letters\n" " --long Long parameter name\n" " -k, --key=VALUE Parameter value\n"; - remove(test_file_name); - test_file = fopen(test_file_name, "w+"); + test_file = tmpfile(); if (test_file == NULL) { goto err_open; } @@ -656,14 +653,12 @@ int option_print(void) goto err_test; } - remove(test_file_name); fclose(test_file); return EXIT_SUCCESS; err_test: err_read: err_seek: - remove(test_file_name); fclose(test_file); err_open: return EXIT_FAILURE;