feat(parsing/sources): adding the sources for parsing's utils
This commit is contained in:
parent
558a482500
commit
cbc402c6a6
1 changed files with 55 additions and 0 deletions
55
parsing/sources/utils.c
Normal file
55
parsing/sources/utils.c
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* utils.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/03/28 12:40:08 by rparodi #+# #+# */
|
||||
/* Updated: 2026/03/28 15:22:10 by rparodi ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "macro.h"
|
||||
#include "parsing.h"
|
||||
#include <ctype.h>
|
||||
#include <errno.h>
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
size_t args_size()
|
||||
{
|
||||
size_t i = 0;
|
||||
while (_flags[i].short_option != 0 || _flags[i].long_option != NULL) {
|
||||
i++;
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
static bool _is_valid_number(const char *str) {
|
||||
if (!str)
|
||||
return false;
|
||||
|
||||
for (size_t i = 0; str[i]; i++) {
|
||||
if (!isdigit(str[i]))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
uint64_t check_num_arguments(char *arg)
|
||||
{
|
||||
if (!_is_valid_number(arg)) {
|
||||
ERROR_LOG(strerror(EINVAL));
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
errno = 0;
|
||||
uint64_t value = strtoull(arg, NULL, 10);
|
||||
if (errno != 0 || value > UINT64_MAX) {
|
||||
ERROR_LOG(strerror(errno))
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue