Cub3D
Loading...
Searching...
No Matches
parse_general_func.c
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* parse_general_func.c :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: kamitsui <kamitsui@student.42tokyo.jp> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2024/11/15 18:52:53 by kamitsui #+# #+# */
9/* Updated: 2024/12/29 18:17:24 by kamitsui ### ########.fr */
10/* */
11/* ************************************************************************** */
12
13#include "cub3d.h"
14
15int print_until_ch(int fd, const char *str, int c)
16{
17 size_t len;
18 ssize_t result;
19 const char *end;
20
21 if (str == NULL)
22 return (EXIT_FAILURE);
23 end = ft_strchr(str, c);
24 if (end == NULL)
25 len = ft_strlen(str);
26 else
27 len = end - str;
28 result = write(fd, str, len);
29 if (result == -1)
30 {
31 perror("write failed");
32 exit(EXIT_FAILURE);
33 }
34 else if ((size_t)result != len)
35 {
36 ft_eprintf("Partial write. Only %lu bytes out of %lu were written.\n",
37 (size_t)result, len);
38 exit(EXIT_FAILURE);
39 }
40 return (EXIT_SUCCESS);
41}
42
51int check_for_not_matching_bit(int variable, int flags_to_check)
52{
53 if ((variable & flags_to_check) == flags_to_check)
54 return (0x00);
55 return (flags_to_check & ~variable);
56}
57
58char *find_next_line(const char *contents)
59{
60 char *line;
61
62 line = ft_strchr(contents, '\n');
63 if (line == NULL)
64 return (NULL);
65 line++;
66 if (*line == '\0')
67 return (NULL);
68 return (line);
69}
70
71bool is_key_line(const char *line, const char *key)
72{
73 size_t len;
74
75 len = ft_strlen(key);
76 return (ft_strncmp(line, key, len) == 0);
77}
78
79char *strdup_trimmed_line(const char *str)
80{
81 const char *start;
82 const char *end;
83 size_t length;
84 char *result;
85
86 start = str;
87 while (*str && *str != '\n')
88 str++;
89 end = str - 1;
90 while (end >= start && (!ft_isprint(*end) || ft_isspace(*end)))
91 end--;
92 length = (end >= start) * (end - start + 1);
93 result = ft_strndup(start, length);
94 return (result);
95}