Skip to content

Options

Ilya Kashnitskiy edited this page Jan 23, 2020 · 2 revisions

A simple module for reading application launch options.

ft_options.h ft_options.c

  • Functions

    • int ft_is_option(const char *line)

If (line[0] == '-') returns 1 else returns 0.

  • char ft_check_opt(char *line, char *base)

Checks the line for valid. If the line contains characters other than characters in the base returns first wrong symbol. 0 otherwise. line should not include start -!

  • t_options ft_get_opt_bit(const char c)

Return bit for particular symbol c.

  • t_options ft_get_options(const char *line)

Returns line decoding.

  • Example

# define VALID_OPTIONS "1Rl"

t_options	read_options(int argc, char **argv)
{
	t_options	opt = 0;
	char		tmp;

	for (int i = 1; i < argc; i++)
	{
		if (ft_is_option(argv[i]))
		{
			if ((tmp = ft_check_opt(argv[i] + 1, VALID_OPTIONS)))
				ft_printf("Wrong options: %c\n", tmp);
			else
				opt |= ft_get_options(argv[i]);
		}
	}
	return (opt);
}

int		main(int argc, char **argv)
{
	t_options	opt;
	ft_memman_init();

	opt = read_options(argc, argv);
	if (ft_opt_test(opt, 'l'))
		ft_printf("Option 'l' is set!\n");
	if (ft_opt_test(opt, 'R'))
		ft_printf("Option 'R' is set!\n");
	if (ft_opt_test(opt, '1'))
		ft_printf("Option '1' is set!\n");

	ft_force_buff();
	ft_memman_clean();
	return (0);
}

Output:

> ./test/test -l
Option 'l' is set!
> ./test/test -l -R -1
Option 'l' is set!
Option 'R' is set!
Option '1' is set!
> ./test/test -l -R -1 -1l -RRRR11ll
Option 'l' is set!
Option 'R' is set!
Option '1' is set!
> ./test/test -l -R -1 file1
Option 'l' is set!
Option 'R' is set!
Option '1' is set!
> ./test/test -l -R -1 file1 -w
Wrong options: w
Option 'l' is set!
Option 'R' is set!
Option '1' is set!

GO TO NEXT PAGE

Clone this wiki locally