lib

morphux C utility library
Log | Files | Refs | Submodules | README | LICENSE | git clone https://git.ne02ptzero.me/git/lib

commit 767e694345429b30f99ee0add6e6d82eddd6fb54
parent fee5ba39482808ac00a1a659be3e688895abb1c8
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Tue, 17 Jan 2017 20:12:46 +0100

Add(Infos): Add basic informations handling

Diffstat:
Minc/m_args.h | 2++
Ainc/m_infos.h | 32++++++++++++++++++++++++++++++++
Minc/morphux.h | 1+
Msrc/m_args.c | 18++++++++++++++++--
Asrc/m_infos.c | 74++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 125 insertions(+), 2 deletions(-)

diff --git a/inc/m_args.h b/inc/m_args.h @@ -20,6 +20,7 @@ # include <stdbool.h> # include <m_types.h> # include <m_print.h> +# include <m_infos.h> typedef struct s_args { /** @@ -54,5 +55,6 @@ typedef struct s_args { u32_t read_opt(const int ac, char **av, const margs_t *args); static void opt_help(const margs_t *args); +static void p_version(void); #endif /* M_ARGS_H */ diff --git a/inc/m_infos.h b/inc/m_infos.h @@ -0,0 +1,32 @@ +/*********************************** LICENSE **********************************\ +* Copyright 2017 Morphux * +* * +* Licensed under the Apache License, Version 2.0 (the "License"); * +* you may not use this file except in compliance with the License. * +* You may obtain a copy of the License at * +* * +* http://www.apache.org/licenses/LICENSE-2.0 * +* * +* Unless required by applicable law or agreed to in writing, software * +* distributed under the License is distributed on an "AS IS" BASIS, * +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * +* See the License for the specific language governing permissions and * +* limitations under the License. * +\******************************************************************************/ + +#ifndef M_INFOS_H +# define M_INFOS_H + +# include <stdio.h> +# include <string.h> + +# define INFOS_G_LEN_MAX 150 + +void set_program_name(const char *str); +void set_version(const char *str); +void set_maintainer(const char *str); +const char *get_program_name(void); +const char *get_version(void); +const char *get_maintainer(void); + +#endif /* M_INFOS_H */ diff --git a/inc/morphux.h b/inc/morphux.h @@ -20,5 +20,6 @@ # include <m_types.h> # include <m_print.h> # include <m_args.h> +# include <m_infos.h> #endif /* MORPHUX_H */ diff --git a/src/m_args.c b/src/m_args.c @@ -58,9 +58,11 @@ u32_t read_opt(const int ac, char **av, const margs_t *args) { continue ; } - /* Help option */ + /* Builtins options */ if (av[i][1] == 'h') opt_help(args); + else if (av[i][1] == 'v') + p_version(); /* Search the option in the args array */ for (it = 0; args[it].opt != 0 && args[it].opt != av[i][1]; it++) @@ -93,8 +95,11 @@ u32_t read_opt(const int ac, char **av, const margs_t *args) { continue ; } + /* Builtins options */ if (strcmp(&(av[i][2]), "help") == 0) opt_help(args); + else if (strcmp(&(av[i][2]), "version") == 0) + p_version(); /* Look for an argument */ for (k = 2; av[i][k] != '\0' && av[i][k] != '='; k++) @@ -137,7 +142,6 @@ u32_t read_opt(const int ac, char **av, const margs_t *args) { * \param args List of arguments to print */ static void opt_help(const margs_t *args) { - m_info("Usage: %s -qwerty\n", "./builder"); m_info("Help:\n"); for (u32_t i = 0; args[i].opt != 0; i++) { m_info("\t-%c | --%s : %s\n", args[i].opt, args[i].s_opt, args[i].desc); @@ -148,3 +152,13 @@ static void opt_help(const margs_t *args) { m_info("\t--option=value\n"); _exit(1); } + +/*! + * \brief Print the program name, the version and the maintainer, then exit + */ +static void p_version(void) { + m_info("Program: %s\n", get_program_name()); + m_info("Version: %s\n", get_version()); + m_info("%s\n", get_maintainer()); + _exit(1); +} diff --git a/src/m_infos.c b/src/m_infos.c @@ -0,0 +1,74 @@ +/*********************************** LICENSE **********************************\ +* Copyright 2017 Morphux * +* * +* Licensed under the Apache License, Version 2.0 (the "License"); * +* you may not use this file except in compliance with the License. * +* You may obtain a copy of the License at * +* * +* http://www.apache.org/licenses/LICENSE-2.0 * +* * +* Unless required by applicable law or agreed to in writing, software * +* distributed under the License is distributed on an "AS IS" BASIS, * +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * +* See the License for the specific language governing permissions and * +* limitations under the License. * +\******************************************************************************/ + +#include <m_infos.h> + + +/* Globals */ +static char program[INFOS_G_LEN_MAX] = ""; +static char version[INFOS_G_LEN_MAX] = ""; +static char maintainer[INFOS_G_LEN_MAX] = ""; + +/*! + * Set the program name to a string + */ +void set_program_name(const char *str) { + memcpy(program, str, strlen(str)); +} + +/*! + * Set the version name to a string + */ +void set_version(const char *str) { + memcpy(version, str, strlen(str)); +} + +/*! + * Set the maintainer name to a string + */ +void set_maintainer(const char *str) { + memcpy(maintainer, str, strlen(str)); +} + +/*! + * Get the program name + * \note If the program is not set, return NULL + */ +const char *get_program_name(void) { + if (strlen(program) != 0) + return program; + return NULL; +} + +/*! + * Get the version + * \note If the version is not set, return NULL + */ +const char *get_version(void) { + if (strlen(version) != 0) + return version; + return NULL; +} + +/*! + * Get the maintainer + * \note If the maintainer is not set, return NULL + */ +const char *get_maintainer(void) { + if (strlen(maintainer) != 0) + return maintainer; + return NULL; +}