Added libft and mlx

This commit is contained in:
Théo Champion 2025-05-03 22:54:55 +02:00
parent 8448ae3a23
commit 82d06d234a
122 changed files with 10400 additions and 0 deletions

40
libft/Makefile Normal file
View file

@ -0,0 +1,40 @@
CC=cc
CFLAGS=-Wall -Wextra -Werror -c
SOURCEFILES=src/str/ft_atoi.c src/mem/ft_bzero.c src/mem/ft_calloc.c src/cond/ft_isalnum.c \
src/cond/ft_isalpha.c src/cond/ft_isascii.c src/cond/ft_isdigit.c \
src/cond/ft_isprint.c src/str/ft_itoa.c src/lst/ft_lstadd_back_bonus.c \
src/lst/ft_lstadd_front_bonus.c src/lst/ft_lstclear_bonus.c \
src/lst/ft_lstdelone_bonus.c src/lst/ft_lstiter_bonus.c \
src/lst/ft_lstlast_bonus.c src/lst/ft_lstmap_bonus.c \
src/lst/ft_lstnew_bonus.c src/lst/ft_lstsize_bonus.c src/mem/ft_memchr.c \
src/mem/ft_memcmp.c src/mem/ft_memcpy.c src/mem/ft_memmove.c src/mem/ft_memset.c \
src/printf/ft_printf.c src/io/ft_putchar_fd.c src/io/ft_putendl_fd.c \
src/io/ft_putnbr_fd.c src/io/ft_putstr_fd.c src/str/ft_split.c \
src/str/ft_strchr.c src/str/ft_strdup.c src/str/ft_striteri.c \
src/str/ft_strjoin.c src/str/ft_strlcat.c src/str/ft_strlcpy.c \
src/str/ft_strlen.c src/str/ft_strmapi.c src/str/ft_strncmp.c \
src/str/ft_strnstr.c src/str/ft_strrchr.c src/str/ft_strtrim.c \
src/str/ft_substr.c src/str/ft_tolower.c src/str/ft_toupper.c \
src/gnl/get_next_line.c src/gnl/get_next_line_utils.c src/printf/printhex.c \
src/printf/printnumbers.c src/printf/printptr.c
OBJECTS=$(patsubst src/%.c,objects/%.o,$(SOURCEFILES))
OBJDIR=objects
NAME=libft.a
all: $(OBJECTS) $(NAME)
$(NAME): $(OBJECTS)
ar -rcs libft.a $(OBJECTS)
$(OBJDIR)/%.o: src/%.c
$(CC) $(CFLAGS) $< -o $@
clean:
rm -f $(OBJECTS)
fclean: clean
rm -f $(NAME)
re: fclean all
.PHONY: all clean fclean re

11
libft/README.md Normal file
View file

@ -0,0 +1,11 @@
# Updated LibFT
For my projects @ 42 Le Havre.
# Todo
- [x] Add ft_printf
- [x] Add get_next_line
- [ ] Rework the file structure (ie. src/string src/list src/unga_bunga)
- [ ] Add a way to manage memory in a more simple way
- [ ] Add other libc functions
- [ ] Add test suite
- [ ] idk, continue this list I guess.

View file

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/31 16:29:36 by tchampio #+# #+# */
/* Updated: 2024/11/04 15:30:25 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FT_PRINTF_H
# define FT_PRINTF_H
# include <stdarg.h>
# include <stdlib.h>
# include <stdint.h>
# include <unistd.h>
# include "../includes/libft.h"
# include "../includes/ft_printf.h"
int ft_printf(const char *format, ...);
char *dec_to_hexa_conversion(int decimal_Number);
int ft_print_ptr_nb(uintptr_t decimal_Number);
int ft_print_ptr(void *ptr);
int ft_print_integer(int i);
int ft_print_uinteger(unsigned int i);
int ft_print_hex(unsigned int num, const char format);
void ft_put_hex(unsigned int num, const char format);
int ft_hex_len(unsigned int num);
#endif

View file

@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/25 10:08:13 by tchampio #+# #+# */
/* Updated: 2024/12/02 17:20:24 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef GET_NEXT_LINE_H
# define GET_NEXT_LINE_H
# include <unistd.h>
# include <stddef.h>
# include <stdlib.h>
# ifndef BUFFER_SIZE
# define BUFFER_SIZE 69
# endif
char *_get_next_line(int fd);
char *_ft_strchr(const char *s, int c);
void *_ft_memcpy(void *dest, const void *src, size_t n);
size_t _ft_strlen(const char *s);
void _ft_strcat(char **dst_ptr, const char *src);
#endif

75
libft/includes/libft.h Normal file
View file

@ -0,0 +1,75 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* libft.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/14 12:40:57 by tchampio #+# #+# */
/* Updated: 2024/12/18 04:40:53 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef LIBFT_H
# define LIBFT_H
# include <stddef.h>
# include <stdarg.h>
# include <stdlib.h>
# include <stdint.h>
# include <stdbool.h>
# include <unistd.h>
typedef struct s_list
{
void *content;
struct s_list *next;
} t_list;
int ft_isalpha(int c);
int ft_isdigit(int c);
int ft_isalnum(int c);
int ft_isascii(int c);
int ft_isprint(int c);
size_t ft_strlen(const char *s);
void *ft_memset(void *s, int c, size_t n);
void ft_bzero(void *s, size_t n);
void *ft_memcpy(void *dest, const void *src, size_t n);
void *ft_memmove(void *dest, const void *src, size_t n);
size_t ft_strlcpy(char *dest, const char *src, size_t n);
size_t ft_strlcat(char *dest, const char *src, size_t n);
char *ft_strchr(const char *str, int c);
char *ft_strrchr(char *str, int c);
int ft_toupper(int c);
int ft_tolower(int c);
int ft_strncmp(const char *s1, const char *s2, size_t n);
void *ft_memchr(void *s, int c, size_t n);
int ft_memcmp(const void *s1, const void *s2, size_t n);
char *ft_strnstr(const char *big, const char *little, size_t n);
int ft_atoi(const char *s);
void *ft_calloc(size_t memsize, size_t count);
char *ft_strdup(const char *s);
char *ft_substr(const char *s, unsigned int start, size_t len);
char *ft_strjoin(const char *s1, const char *s2);
char *ft_strtrim(const char *s1, const char *set);
char **ft_split(const char *s, char separator);
char *ft_itoa(int n);
char *ft_strmapi(const char *s, char (*f)(unsigned int, char));
void ft_striteri(char *s, void (*f)(unsigned int, char *));
void ft_putchar_fd(char c, int fd);
void ft_putendl_fd(char *s, int fd);
void ft_putstr_fd(char *s, int fd);
void ft_putnbr_fd(int s, int fd);
t_list *ft_lstnew(void *content);
void ft_lstadd_front(t_list **lst, t_list *new);
int ft_lstsize(t_list *lst);
t_list *ft_lstlast(t_list *lst);
void ft_lstadd_back(t_list **lst, t_list *new);
void ft_lstdelone(t_list *lst, void (*del)(void *));
void ft_lstclear(t_list **lst, void (*del)(void *));
void ft_lstiter(t_list *lst, void (*f)(void *));
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *));
char *get_next_line(int fd);
int ft_printf(const char *format, ...);
#endif

0
libft/objects/.gitkeep Normal file
View file

View file

View file

View file

View file

View file

View file

View file

View file

@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isalnum.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/14 14:56:25 by tchampio #+# #+# */
/* Updated: 2024/10/14 15:03:40 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
int ft_isalnum(int c)
{
if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z')
|| (c >= 'A' && c <= 'Z'))
return (1);
return (0);
}

View file

@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isalpha.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/14 13:38:13 by tchampio #+# #+# */
/* Updated: 2024/10/14 13:55:09 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
int ft_isalpha(int c)
{
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
return (1);
else
return (0);
}

View file

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isascii.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/14 15:07:15 by tchampio #+# #+# */
/* Updated: 2024/10/14 15:27:27 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
int ft_isascii(int c)
{
if (c >= 0 && c <= 127)
return (1);
return (0);
}

View file

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isdigit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/14 13:57:00 by tchampio #+# #+# */
/* Updated: 2024/10/14 14:52:41 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
int ft_isdigit(int c)
{
if (c >= '0' && c <= '9')
return (1);
return (0);
}

View file

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isprint.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/14 15:07:15 by tchampio #+# #+# */
/* Updated: 2024/10/14 15:30:57 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
int ft_isprint(int c)
{
if (c >= 32 && c <= 126)
return (1);
return (0);
}

View file

@ -0,0 +1,106 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/30 11:15:38 by tchampio #+# #+# */
/* Updated: 2024/12/02 17:19:36 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/get_next_line.h"
void getlinefrombuffer(char **line, char *buffer)
{
size_t remaining;
char *newline_pos;
char saved_char;
newline_pos = _ft_strchr(buffer, '\n');
if (newline_pos)
{
saved_char = newline_pos[1];
newline_pos[1] = '\0';
_ft_strcat(line, buffer);
if (*line == NULL)
return ;
newline_pos[1] = saved_char;
newline_pos++;
}
else
{
_ft_strcat(line, buffer);
if (*line == NULL)
return ;
newline_pos = buffer + _ft_strlen(buffer);
}
remaining = _ft_strlen(newline_pos);
_ft_memcpy(buffer, newline_pos, remaining);
buffer[remaining] = '\0';
}
char *rereadbuffer(char *buffer)
{
size_t len;
char *line;
line = _ft_strchr(buffer, '\n');
if (line == NULL)
line = _ft_strchr(buffer, '\0');
len = line - buffer + (*line == '\n');
line = (char *)malloc(len + 1);
if (line == NULL)
return (NULL);
_ft_memcpy(line, buffer, len);
line[len] = '\0';
_ft_memcpy(buffer, buffer + len, BUFFER_SIZE - len);
buffer[BUFFER_SIZE - len] = '\0';
return (line);
}
char *readfd(int fd, char *buffer)
{
ssize_t bytes;
char *line;
line = rereadbuffer(buffer);
while (!_ft_strchr(line, '\n'))
{
bytes = read(fd, buffer, BUFFER_SIZE);
if (bytes < 0)
{
free(line);
return (NULL);
}
buffer[bytes] = '\0';
if (bytes == 0 || _ft_strchr(buffer, '\n'))
{
getlinefrombuffer(&line, buffer);
return (line);
}
_ft_strcat(&line, buffer);
if (line == NULL)
return (NULL);
}
return (line);
}
char *get_next_line(int fd)
{
static char buffer[BUFFER_SIZE + 1] = {0};
char *line;
if (fd < 0 || BUFFER_SIZE < 0)
return (NULL);
line = readfd(fd, buffer);
if (line == NULL)
return (NULL);
if (_ft_strlen(line) == 0)
{
free(line);
return (NULL);
}
return (line);
}

View file

@ -0,0 +1,60 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/25 10:09:25 by tchampio #+# #+# */
/* Updated: 2024/12/02 17:20:10 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/get_next_line.h"
char *_ft_strchr(const char *s, int c)
{
while (*s != '\0' && *s != c)
++s;
if (*s != '\0' || c == '\0')
return ((char *)s);
return (NULL);
}
void *_ft_memcpy(void *dest, const void *src, size_t n)
{
size_t i;
i = 0;
while (i++ < n)
*(char *)dest++ = *(char *)src++;
return ((void *)((char *)dest - i));
}
size_t _ft_strlen(const char *s)
{
const char *org = s;
while (*s)
++s;
return (s - org);
}
void _ft_strcat(char **dst_ptr, const char *src)
{
size_t dst_len;
size_t src_len;
char *ans;
dst_len = _ft_strlen(*dst_ptr);
src_len = _ft_strlen(src);
ans = malloc(dst_len + src_len + 1);
if (ans)
{
_ft_memcpy(ans, *dst_ptr, dst_len);
_ft_memcpy(ans + dst_len, src, src_len);
ans[dst_len + src_len] = '\0';
}
free(*dst_ptr);
*dst_ptr = ans;
}

View file

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putchar_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/18 10:13:00 by tchampio #+# #+# */
/* Updated: 2024/10/18 10:21:39 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
void ft_putchar_fd(char c, int fd)
{
write(fd, &c, 1);
}

View file

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putendl_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/18 10:26:53 by tchampio #+# #+# */
/* Updated: 2024/10/18 10:40:11 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
#include "../../includes/libft.h"
void ft_putendl_fd(char *s, int fd)
{
if (!s)
return ;
ft_putstr_fd(s, fd);
ft_putchar_fd('\n', fd);
}

View file

@ -0,0 +1,37 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbr_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/18 10:31:07 by tchampio #+# #+# */
/* Updated: 2024/10/18 10:34:25 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
#include "../../includes/libft.h"
#define MIN_INT -2147483648
void ft_putnbr_fd(int nb, int fd)
{
if (nb == MIN_INT)
{
ft_putchar_fd('-', fd);
ft_putchar_fd('2', fd);
nb = 147483648;
}
if (nb < 0)
{
ft_putchar_fd('-', fd);
nb = -nb;
}
if (nb >= 10)
{
ft_putnbr_fd(nb / 10, fd);
ft_putnbr_fd(nb % 10, fd);
}
else
ft_putchar_fd('0' + nb, fd);
}

View file

@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putstr_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/18 10:23:40 by tchampio #+# #+# */
/* Updated: 2024/10/18 10:28:57 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
#include "../../includes/libft.h"
void ft_putstr_fd(char *s, int fd)
{
size_t size;
if (!s)
return ;
size = ft_strlen(s);
write(fd, s, size);
}

View file

@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstadd_back.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/22 12:56:49 by tchampio #+# #+# */
/* Updated: 2024/10/22 13:21:51 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/libft.h"
void ft_lstadd_back(t_list **lst, t_list *new)
{
t_list *last;
if (lst)
{
if (*lst)
{
last = ft_lstlast(*lst);
last->next = new;
}
else
*lst = new;
}
}

View file

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstadd_front_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/21 13:27:19 by tchampio #+# #+# */
/* Updated: 2024/10/22 13:02:46 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/libft.h"
#include <stdlib.h>
void ft_lstadd_front(t_list **lst, t_list *new)
{
new->next = *lst;
*lst = new;
}

View file

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstclear_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/22 14:31:30 by tchampio #+# #+# */
/* Updated: 2024/10/22 14:44:42 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/libft.h"
#include <stdlib.h>
void ft_lstclear(t_list **lst, void (*del)(void *))
{
t_list *next;
if (!lst || !del || !*lst)
return ;
while (lst && *lst)
{
next = (*lst)->next;
ft_lstdelone(*lst, del);
*lst = next;
}
}

View file

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstdelone_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/22 13:33:02 by tchampio #+# #+# */
/* Updated: 2024/10/22 14:29:59 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/libft.h"
#include <stdlib.h>
void ft_lstdelone(t_list *lst, void (*del)(void *))
{
if (!lst || !del)
return ;
(*del)(lst->content);
free(lst);
}

View file

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstiter_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/22 14:46:27 by tchampio #+# #+# */
/* Updated: 2024/10/22 15:12:21 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/libft.h"
void ft_lstiter(t_list *lst, void (*f)(void *))
{
t_list *current;
if (!lst || !f)
return ;
current = lst;
while (current)
{
(*f)(current->content);
current = current->next;
}
}

View file

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstlast_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/22 12:43:20 by tchampio #+# #+# */
/* Updated: 2024/10/22 13:03:40 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/libft.h"
t_list *ft_lstlast(t_list *lst)
{
t_list *current;
if (!lst)
return (NULL);
current = lst;
while (current->next != NULL)
current = current->next;
return (current);
}

View file

@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstmap_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/23 15:52:26 by tchampio #+# #+# */
/* Updated: 2024/10/23 17:24:57 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/libft.h"
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *))
{
t_list *newlist;
t_list *newnode;
void *content;
if (!lst || !f || !del)
return (NULL);
newlist = NULL;
while (lst)
{
content = f(lst->content);
newnode = ft_lstnew(content);
if (!newnode)
{
ft_lstclear(&newlist, del);
del(content);
return (NULL);
}
ft_lstadd_back(&newlist, newnode);
lst = lst->next;
}
return (newlist);
}

View file

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstnew_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/21 12:51:42 by tchampio #+# #+# */
/* Updated: 2024/10/21 14:12:56 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/libft.h"
#include <stdlib.h>
t_list *ft_lstnew(void *content)
{
t_list *newlist;
newlist = (t_list *)malloc(sizeof(t_list));
if (!newlist)
return (NULL);
newlist->content = content;
newlist->next = NULL;
return (newlist);
}

View file

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstsize_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/22 11:57:52 by tchampio #+# #+# */
/* Updated: 2024/10/22 13:12:56 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/libft.h"
int ft_lstsize(t_list *lst)
{
int i;
i = 0;
while (lst)
{
i++;
lst = lst->next;
}
return (i);
}

18
libft/src/mem/ft_bzero.c Normal file
View file

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_bzero.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/15 09:39:47 by tchampio #+# #+# */
/* Updated: 2024/10/15 10:15:31 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/libft.h"
void ft_bzero(void *s, size_t n)
{
ft_memset(s, '\0', n);
}

31
libft/src/mem/ft_calloc.c Normal file
View file

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_calloc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/16 10:10:41 by tchampio #+# #+# */
/* Updated: 2024/10/18 16:02:48 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "../../includes/libft.h"
#include <limits.h>
#include <stdint.h>
void *ft_calloc(size_t memsize, size_t count)
{
size_t total;
void *allocated;
total = memsize * count;
if (memsize != 0 && total / memsize != count)
return (NULL);
allocated = malloc(total);
if (allocated == NULL)
return (NULL);
ft_bzero(allocated, total);
return (allocated);
}

27
libft/src/mem/ft_memchr.c Normal file
View file

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/15 13:30:59 by tchampio #+# #+# */
/* Updated: 2024/10/15 16:22:14 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include <stddef.h>
char *ft_memchr(void *s, int c, size_t n)
{
size_t i;
i = 0;
while (i < n)
{
if (((unsigned char *)s)[i] == (unsigned char)c)
return ((void *)(s + i));
i++;
}
return (NULL);
}

29
libft/src/mem/ft_memcmp.c Normal file
View file

@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memcmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/17 15:02:03 by tchampio #+# #+# */
/* Updated: 2024/10/23 10:42:19 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/libft.h"
int ft_memcmp(const void *s1, const void *s2, size_t n)
{
size_t i;
i = 0;
while (((unsigned char *)s1 || (unsigned char *)s2) && i < n)
{
if (*(unsigned char *)s1 != *(unsigned char *)s2)
return (*(unsigned char *)s1 - *(unsigned char *)s2);
s1++;
s2++;
i++;
}
return (0);
}

28
libft/src/mem/ft_memcpy.c Normal file
View file

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memcpy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/15 09:58:47 by tchampio #+# #+# */
/* Updated: 2024/10/19 14:24:53 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include <stddef.h>
void *ft_memcpy(void *dest, const void *src, size_t n)
{
size_t i;
if (!dest && !src)
return (NULL);
i = 0;
while (i < n)
{
((unsigned char *)dest)[i] = ((unsigned char *)src)[i];
i++;
}
return (dest);
}

130
libft/src/mem/ft_memmove.c Normal file
View file

@ -0,0 +1,130 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memmove.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/15 10:17:04 by tchampio #+# #+# */
/* Updated: 2024/10/21 09:44:05 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include <stddef.h>
void *ft_memmove(void *dest, const void *src, size_t n)
{
size_t i;
if (n == 0 || dest == src)
return (dest);
if (src < dest)
{
i = n;
while (i > 0)
{
i--;
((unsigned char *)dest)[i] = ((unsigned char *)src)[i];
}
}
else
{
i = 0;
while (i < n)
{
((unsigned char *)dest)[i] = ((unsigned char *)src)[i];
i++;
}
}
return (dest);
}
/*
#include <stdio.h>
#include <string.h>
// Prototype de la fonction ft_memmove que tu devras implémenter
void *ft_memmove(void *dest, const void *src, size_t n);
// Macros pour les couleurs
#define RESET "\033[0m"
#define RED "\033[31m"
#define GREEN "\033[32m"
#define BOLDWHITE "\033[1m\033[37m"
// Fonction de test
void test_ft_memmove(void *dest, const void *src, size_t n,
const void *expected, int *success_count) {
// Utilisation de ft_memmove
ft_memmove(dest, src, n);
// Comparaison des résultats
if (memcmp(dest, expected, n) == 0) {
printf(GREEN "Test passed: ft_memmove(%p, %p, %zu)\n" RESET,
dest, src, n);
(*success_count)++;
} else {
printf(RED "Test failed: ft_memmove(%p, %p, %zu)\nExpected: ",
dest, src, n);
for (size_t i = 0; i < n; i++) {
printf("%02x ", ((unsigned char *)expected)[i]);
}
printf("\nGot: ");
for (size_t i = 0; i < n; i++) {
printf("%02x ", ((unsigned char *)dest)[i]);
}
printf("\n" RESET);
}
}
int main() {
int success_count = 0;
int total_tests = 0;
// Déclaration des buffers
char buffer1[50], buffer2[50];
// Test 1 : Copier une petite chaîne de caractères
memset(buffer1, 0, sizeof(buffer1)); // Nettoyage du buffer
const char *src1 = "Hello";
test_ft_memmove(buffer1, src1, strlen(src1) + 1, src1, &success_count);
total_tests++; // Copie de "Hello" avec '\0'
// Test 2 : Copier une chaîne vide
memset(buffer1, 0, sizeof(buffer1)); // Nettoyage du buffer
const char *src2 = "";
test_ft_memmove(buffer1, src2, 1, src2, &success_count);
total_tests++; // Copie de la chaîne vide
// Test 3 : Copier un tableau de chiffres
memset(buffer1, 0, sizeof(buffer1)); // Nettoyage du buffer
const char src3[] = {1, 2, 3, 4, 5};
const char expected3[] = {1, 2, 3, 4, 5};
test_ft_memmove(buffer1, src3, sizeof(src3), expected3, &success_count);
total_tests++; // Copie du tableau {1, 2, 3, 4, 5}
// Test 4 : Cas de chevauchement (src avant dest)
memset(buffer1, 0, sizeof(buffer1)); // Nettoyage du buffer
const char overlap_src1[] = "Overlap Test";
memcpy(buffer1, overlap_src1, sizeof(overlap_src1));
test_ft_memmove(buffer1 + 5, buffer1, strlen(overlap_src1) + 1,
buffer1 + 5, &success_count);
total_tests++; // Chevauchement : src est avant dest
// Test 5 : Cas de chevauchement (dest avant src)
memset(buffer1, 0, sizeof(buffer1)); // Nettoyage du buffer
memcpy(buffer1, overlap_src1, sizeof(overlap_src1));
test_ft_memmove(buffer1, buffer1 + 5, 10, buffer1, &success_count);
total_tests++; // Chevauchement : dest est avant src
// Test 6 : Copier 0 octets (ne doit rien changer)
strcpy(buffer2, "No change");
test_ft_memmove(buffer2, "Nothing", 0, "No change", &success_count);
total_tests++; // Copier 0 octets, doit rester inchangé
// Résumé des résultats
printf(BOLDWHITE "\nTotal tests: %d, Success: %d, Failures: %d\n" RESET,
total_tests, success_count, total_tests - success_count);
return 0;
}
*/

105
libft/src/mem/ft_memset.c Normal file
View file

@ -0,0 +1,105 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memset.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/14 15:56:53 by tchampio #+# #+# */
/* Updated: 2024/10/14 17:27:38 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include <stddef.h>
void *ft_memset(void *s, int c, size_t n)
{
size_t i;
i = 0;
while (i < n)
{
((char *)s)[i] = c;
i++;
}
return (s);
}
/*
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
// Prototype de la fonction ft_memset que tu devras implémenter
void *ft_memset(void *s, int c, size_t n);
// Macros pour les couleurs
#define RESET "\033[0m"
#define RED "\033[31m"
#define GREEN "\033[32m"
#define BOLDWHITE "\033[1m\033[37m"
// Fonction de test
void test_ft_memset(void *s, int c, size_t n, const char *expected,
int *success_count) {
// Utilisation de ft_memset
ft_memset(s, c, n);
// Comparaison des résultats
if (memcmp(s, expected, n) == 0) {
printf(GREEN "Test passed: ft_memset(%p, %d, %zu)\n" RESET, s, c, n);
(*success_count)++;
} else {
printf(RED "Test failed: ft_memset(%p, %d, %zu)\nExpected: %s,
Got: ", s, c, n, expected);
for (size_t i = 0; i < n; i++) {
printf("%02x ", ((unsigned char *)s)[i]);
}
printf("\n" RESET);
}
}
int main() {
int success_count = 0;
int total_tests = 0;
// Tests de ft_memset
char buffer[50];
// Test 1 : Remplir un tampon avec un seul caractère
memset(buffer, 0, sizeof(buffer)); // Reset buffer
test_ft_memset(buffer, 'A', 5, "AAAAA", &success_count);
total_tests++;
// Test 2 : Remplir avec un caractère null
memset(buffer, 0, sizeof(buffer)); // Reset buffer
test_ft_memset(buffer, '\0', 5, "\0\0\0\0\0", &success_count);
total_tests++;
// Test 3 : Remplir une partie du tampon
memset(buffer, 0, sizeof(buffer)); // Reset buffer
test_ft_memset(buffer, 'B', 3, "BBB", &success_count);
total_tests++;
// Test 4 : Remplir avec un caractère à la limite de l'ASCII
memset(buffer, 0, sizeof(buffer)); // Reset buffer
test_ft_memset(buffer, 255, 4, "\xFF\xFF\xFF\xFF", &success_count);
total_tests++;
// Test 5 : Remplir tout le tampon
memset(buffer, 0, sizeof(buffer)); // Reset buffer
test_ft_memset(buffer, 'C', sizeof(buffer),
"CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC", &success_count);
total_tests++;
// Test 6 : Remplir 0 octets
memset(buffer, 0, sizeof(buffer)); // Reset buffer
test_ft_memset(buffer, 'D', 0, "", &success_count);
total_tests++;
// Résumé des résultats
printf(BOLDWHITE "\nTotal tests: %d, Success: %d, Failures: %d\n" RESET,
total_tests, success_count, total_tests - success_count);
return 0;
}
*/

View file

@ -0,0 +1,78 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/31 15:01:45 by tchampio #+# #+# */
/* Updated: 2024/11/04 15:28:21 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/ft_printf.h"
static int ft_print_string(char *s)
{
int size;
if (!s)
{
ft_putstr_fd("(null)", 1);
return (6);
}
size = (int)ft_strlen(s);
ft_putstr_fd(s, 1);
return (size);
}
static int ft_check_flag(char flag, va_list args)
{
int added;
added = 0;
if (flag == 'c')
return (ft_putchar_fd(va_arg(args, int), 1), 1);
else if (flag == 's')
added += ft_print_string((char *)va_arg(args, char *));
else if (flag == 'p')
added += ft_print_ptr((void *)va_arg(args, void *));
else if (flag == 'i' || flag == 'd')
added += ft_print_integer((int)va_arg(args, int));
else if (flag == 'u')
added += ft_print_uinteger((unsigned int)va_arg(args, unsigned int));
else if (flag == 'x')
added += ft_print_hex((int)va_arg(args, int), 'x');
else if (flag == 'X')
added += ft_print_hex((int)va_arg(args, int), 'X');
else if (flag == '%')
return (ft_putchar_fd('%', 1), 1);
return (added);
}
int ft_printf(const char *format, ...)
{
int counter;
va_list args;
size_t index;
index = 0;
counter = 0;
va_start(args, format);
while (format[index])
{
if (format[index] == '%')
{
index++;
counter += ft_check_flag(format[index], args);
index++;
}
else
{
ft_putchar_fd(format[index], 1);
counter++;
index++;
}
}
return (counter);
}

View file

@ -0,0 +1,57 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* printhex.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/04 15:25:57 by tchampio #+# #+# */
/* Updated: 2024/11/04 16:46:28 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/ft_printf.h"
#include "../../includes/libft.h"
int ft_hex_len(unsigned int num)
{
int len;
len = 0;
while (num != 0)
{
len++;
num = num / 16;
}
return (len);
}
void ft_put_hex(unsigned int num, const char format)
{
if (num >= 16)
{
ft_put_hex(num / 16, format);
ft_put_hex(num % 16, format);
}
else
{
if (num <= 9)
ft_putchar_fd((num + '0'), 1);
else
{
if (format == 'x')
ft_putchar_fd((num - 10 + 'a'), 1);
if (format == 'X')
ft_putchar_fd((num - 10 + 'A'), 1);
}
}
}
int ft_print_hex(unsigned int num, const char format)
{
if (num == 0)
return (write(1, "0", 1));
else
ft_put_hex(num, format);
return (ft_hex_len(num));
}

View file

@ -0,0 +1,57 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* printnumbers.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/02 05:50:37 by tchampio #+# #+# */
/* Updated: 2024/11/04 16:00:42 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/ft_printf.h"
int ft_print_integer(int i)
{
int size;
char *str;
str = ft_itoa(i);
if (str == NULL)
return (0);
size = ft_strlen(str);
ft_putstr_fd(str, 1);
free(str);
return (size);
}
static int get_number_size(unsigned int i)
{
int size;
if (i == 0)
return (1);
size = 0;
while (i != 0)
{
i /= 10;
size++;
}
return (size);
}
int ft_print_uinteger(unsigned int i)
{
unsigned int nb;
nb = i;
if (nb >= 10)
{
ft_putnbr_fd(nb / 10, 1);
ft_putnbr_fd(nb % 10, 1);
}
else
ft_putchar_fd('0' + nb, 1);
return (get_number_size(i));
}

View file

@ -0,0 +1,55 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* printhex.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/02 05:52:06 by tchampio #+# #+# */
/* Updated: 2024/11/04 15:25:12 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/ft_printf.h"
int ft_print_ptr_nb(uintptr_t decimal_number)
{
long i;
long j;
uintptr_t temp;
char hexa_number[100];
i = 1;
while (decimal_number != 0)
{
temp = decimal_number % 16;
if (temp < 10)
temp = temp + 48;
else
temp = temp + 55;
hexa_number[i++] = temp;
decimal_number = decimal_number / 16;
}
j = i - 1;
while (j > 0)
{
ft_putchar_fd(ft_tolower(hexa_number[j--]), 1);
}
return (i - 1);
}
int ft_print_ptr(void *ptr)
{
int size;
uintptr_t addr;
if (ptr == NULL)
{
ft_putstr_fd("(nil)", 1);
return (5);
}
addr = (uintptr_t)ptr;
ft_putstr_fd("0x", 1);
size = ft_print_ptr_nb(addr) + 2;
return (size);
}

37
libft/src/str/ft_atoi.c Normal file
View file

@ -0,0 +1,37 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/15 17:02:23 by tchampio #+# #+# */
/* Updated: 2024/10/15 17:23:50 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/libft.h"
int ft_atoi(const char *s)
{
int isnegative;
int res;
res = 0;
isnegative = 1;
while (*s == ' ' || *s == '\f' || *s == '\r' || *s == '\v' || *s == '\t'
|| *s == '\n')
s++;
if (*s == '-' || *s == '+')
{
if (*s == '-')
isnegative = -1;
s++;
}
while (*s >= '0' && *s <= '9')
{
res = (res * 10) + (*s - '0');
s++;
}
return (res * isnegative);
}

72
libft/src/str/ft_itoa.c Normal file
View file

@ -0,0 +1,72 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/17 11:29:14 by tchampio #+# #+# */
/* Updated: 2024/10/21 10:13:23 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/libft.h"
#include <stdlib.h>
static size_t getnumberoffigures(int n)
{
size_t totalsize;
long nn;
totalsize = 0;
nn = n;
if (n == 0)
return (1);
if (nn < 0)
{
totalsize++;
nn = -nn;
}
while (nn > 0)
{
nn /= 10;
totalsize++;
}
return (totalsize);
}
static void populate_array(char *s, int n, size_t size)
{
int isneg;
long nn;
size_t index;
nn = n;
if (nn < 0)
nn = -nn;
isneg = (n < 0);
index = size - 1;
while (nn > 0)
{
s[index] = (nn % 10) + '0';
nn /= 10;
index--;
}
if (isneg == 1)
s[index] = '-';
}
char *ft_itoa(int n)
{
char *s;
size_t size;
size = getnumberoffigures(n);
s = (char *)ft_calloc(sizeof(*s), size + 1);
if (!s)
return (NULL);
if (n == 0)
ft_strlcpy(s, "0", 2);
populate_array(s, n, size);
return (s);
}

87
libft/src/str/ft_split.c Normal file
View file

@ -0,0 +1,87 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/17 09:23:50 by tchampio #+# #+# */
/* Updated: 2024/10/23 13:16:12 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/libft.h"
#include <stdlib.h>
static size_t count_words(const char *s, char sep)
{
size_t count;
count = 0;
if (!s)
return (0);
while (*s)
{
while (*s == sep)
s++;
if (*s)
count++;
while (*s != sep && *s)
s++;
}
return (count);
}
static int free_all(char **split, size_t words)
{
size_t i;
i = 0;
while (i < words)
free(split[i++]);
free(split);
return (1);
}
static int populate(char **split, const char *s, char sep)
{
size_t wordlength;
size_t i;
wordlength = 0;
i = 0;
while (*s)
{
while (*s == sep && *s)
s++;
if (*s)
{
if (!ft_strchr(s, sep))
wordlength = ft_strlen(s);
else
wordlength = ft_strchr(s, sep) - s;
split[i] = ft_substr(s, 0, wordlength);
if (split[i] == NULL)
return (free_all(split, i));
i++;
s += wordlength;
}
}
split[i] = NULL;
return (0);
}
char **ft_split(const char *s, char separator)
{
char **split;
if (!s)
return (NULL);
split = (char **)malloc(sizeof(char *)
* (count_words(s, separator) + 1));
if (!split)
return (NULL);
if (populate(split, s, separator) == 1)
return (NULL);
return (split);
}

31
libft/src/str/ft_strchr.c Normal file
View file

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/15 13:30:59 by tchampio #+# #+# */
/* Updated: 2024/10/19 13:41:02 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include <stddef.h>
char *ft_strchr(const char *str, int c)
{
char *s;
char tofind;
tofind = (char)c;
s = (char *)str;
while (*s)
{
if (*s == tofind)
return (s);
s++;
}
if (*s == tofind)
return (s);
return (NULL);
}

27
libft/src/str/ft_strdup.c Normal file
View file

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strdup.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/16 10:34:42 by tchampio #+# #+# */
/* Updated: 2024/10/19 14:52:38 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/libft.h"
#include <stdlib.h>
char *ft_strdup(const char *s)
{
size_t length;
char *str;
length = ft_strlen(s) + 1;
str = (char *)malloc(sizeof(*str) * length);
if (!str)
return (NULL);
ft_memcpy((char *)str, (const char *)s, length);
return (str);
}

View file

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_striteri.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/18 09:54:48 by tchampio #+# #+# */
/* Updated: 2024/10/18 10:09:15 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/libft.h"
void ft_striteri(char *s, void (*f)(unsigned int, char*))
{
size_t i;
if (!s || !f)
return ;
i = 0;
while (*s)
{
(*f)(i, s);
i++;
s++;
}
}

View file

@ -0,0 +1,42 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strjoin.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/16 15:40:16 by tchampio #+# #+# */
/* Updated: 2024/10/18 15:47:12 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/libft.h"
#include <stdlib.h>
char *ft_strjoin(const char *s1, const char *s2)
{
size_t lengths1;
size_t lengths2;
size_t index;
char *str;
lengths1 = ft_strlen(s1);
lengths2 = ft_strlen(s2);
index = 0;
str = (char *)ft_calloc(sizeof(*str), (lengths1 + lengths2 + 1));
if (str == NULL)
return (NULL);
while (*s1)
{
str[index] = *s1;
s1++;
index++;
}
while (*s2)
{
str[index] = *s2;
s2++;
index++;
}
return (str);
}

View file

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlcat.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/15 12:31:51 by tchampio #+# #+# */
/* Updated: 2024/10/18 11:18:01 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/libft.h"
size_t ft_strlcat(char *dest, const char *src, size_t size)
{
size_t i;
size_t destsize;
size_t srcsize;
i = 0;
destsize = ft_strlen(dest);
srcsize = ft_strlen(src);
if (size <= 0 || destsize >= size)
return (srcsize + size);
while (i < size - destsize - 1 && src[i])
{
dest[destsize + i] = src[i];
i++;
}
dest[destsize + i] = '\0';
return (destsize + srcsize);
}

View file

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlcpy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/15 11:03:52 by tchampio #+# #+# */
/* Updated: 2024/10/17 12:33:16 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include <stddef.h>
#include "../../includes/libft.h"
size_t ft_strlcpy(char *dest, const char *src, size_t size)
{
size_t i;
size_t srcsize;
i = 0;
srcsize = ft_strlen(src);
if (size == 0)
return (srcsize);
while (i < size - 1 && src[i] != '\0')
{
dest[i] = src[i];
i++;
}
dest[i] = '\0';
return (srcsize);
}

81
libft/src/str/ft_strlen.c Normal file
View file

@ -0,0 +1,81 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlen.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/14 15:36:45 by tchampio #+# #+# */
/* Updated: 2024/10/14 15:53:37 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include <stddef.h>
size_t ft_strlen(const char *s)
{
int len;
len = 0;
while (*s)
{
s++;
len++;
}
return (len);
}
/*
#include <stdio.h>
#include <string.h>
#include <limits.h>
// Prototype de la fonction ft_strlen que tu devras implémenter
size_t ft_strlen(const char *s);
// Macros pour les couleurs
#define RESET "\033[0m"
#define RED "\033[31m"
#define GREEN "\033[32m"
#define BOLDWHITE "\033[1m\033[37m"
// Fonction de test
void test_ft_strlen(const char *s, size_t expected, int *success_count) {
size_t result = ft_strlen(s);
if (result == expected) {
printf(GREEN "Test passed: ft_strlen(\"%s\") == %zu\n" RESET, s, result);
(*success_count)++;
} else {
printf(RED "Test failed: ft_strlen(\"%s\") == %zu (expected %zu)\n"
RESET, s, result, expected);
}
}
int main() {
int success_count = 0;
int total_tests = 0;
// Tests de chaînes avec des longueurs différentes
test_ft_strlen("", 0, &success_count); total_tests++;
test_ft_strlen("a", 1, &success_count); total_tests++;
test_ft_strlen("Hello", 5, &success_count); total_tests++;
test_ft_strlen("Hello, World!", 13, &success_count); total_tests++;
// Tests de chaînes avec des caractères spéciaux
test_ft_strlen("12345", 5, &success_count); total_tests++;
test_ft_strlen("!@#$%^&*()", 10, &success_count); total_tests++;
test_ft_strlen(" \t\n\r", 4, &success_count); total_tests++;
// Tests de longues chaînes
const char long_string[] = "This is a long string to test the
function ft_strlen properly.";
test_ft_strlen(long_string, strlen(long_string), &success_count);
total_tests++;
// Résumé des résultats
printf(BOLDWHITE "\nTotal tests: %d, Success: %d, Failures: %d\n" RESET,
total_tests, success_count, total_tests - success_count);
return 0;
}
*/

View file

@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strmapi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/17 15:58:52 by tchampio #+# #+# */
/* Updated: 2024/10/18 10:09:39 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/libft.h"
#include <stdlib.h>
char *ft_strmapi(char const *s, char (*f)(unsigned int, char))
{
size_t i;
char *str;
if (!s || !f)
return (NULL);
i = 0;
str = (char *)malloc(ft_strlen(s) + 1);
if (str == NULL)
return (NULL);
while (s[i])
{
str[i] = (*f)(i, s[i]);
i++;
}
str[i] = '\0';
return (str);
}

View file

@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strncmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/15 14:16:56 by tchampio #+# #+# */
/* Updated: 2024/10/19 14:49:00 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include <stddef.h>
int ft_strncmp(const char *s1, const char *s2, size_t n)
{
size_t i;
i = 0;
while ((*s1 || *s2) && i < n)
{
if (*s1 != *s2)
return ((unsigned char)*s1 - (unsigned char)*s2);
s1++;
s2++;
i++;
}
return (0);
}

View file

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strnstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/15 16:33:24 by tchampio #+# #+# */
/* Updated: 2024/10/18 12:39:00 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/libft.h"
char *ft_strnstr(const char *big, const char *little, size_t len)
{
size_t littlesize;
size_t i;
if (little[0] == '\0')
return ((char *)big);
littlesize = ft_strlen(little);
i = 0;
while (*big && i < len)
{
if (ft_strncmp(big, little, littlesize) == 0
&& (i + littlesize) <= len)
return ((char *)big);
big++;
i++;
}
return (NULL);
}

View file

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strrchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/15 13:44:15 by tchampio #+# #+# */
/* Updated: 2024/10/18 10:55:22 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include <stddef.h>
char *ft_strrchr(char *str, int c)
{
char *s;
char *last;
char tofind;
s = str;
last = NULL;
tofind = (char)c;
while (*s)
{
if (*s == tofind)
last = s;
s++;
}
if (*s == tofind)
last = s;
return (last);
}

View file

@ -0,0 +1,51 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/16 15:54:04 by tchampio #+# #+# */
/* Updated: 2024/10/19 11:06:07 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/libft.h"
#include <stdio.h>
#include <stdlib.h>
static int is_in_set(char c, const char *set)
{
while (*set)
{
if (*set == c)
return (1);
set++;
}
return (0);
}
char *ft_strtrim(const char *s1, const char *set)
{
size_t start;
size_t end;
size_t s1_len;
char *trim;
if (!s1 || !set)
return (NULL);
start = 0;
while (is_in_set(s1[start], set))
start++;
s1_len = ft_strlen(s1);
end = s1_len - 1;
if (start == s1_len)
return (ft_strdup(""));
while (is_in_set(s1[end], set))
end--;
trim = malloc((end - start + 2) * sizeof(char));
if (!trim)
return (NULL);
ft_strlcpy(trim, (s1 + start), (end - start + 2));
return (trim);
}

36
libft/src/str/ft_substr.c Normal file
View file

@ -0,0 +1,36 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/16 10:57:55 by tchampio #+# #+# */
/* Updated: 2024/10/19 15:35:35 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "../../includes/libft.h"
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *str;
size_t strlen;
size_t allocated;
if (!s)
return (NULL);
strlen = ft_strlen(s);
if (start > strlen)
return (ft_strdup(""));
if (strlen - start >= len)
allocated = len + 1;
else
allocated = (strlen - start) + 1;
str = (char *)malloc(sizeof(*str) * allocated);
if (!str)
return (NULL);
ft_strlcpy(str, s + start, allocated);
return (str);
}

View file

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_tolower.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/15 13:17:58 by tchampio #+# #+# */
/* Updated: 2024/10/16 16:51:33 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/libft.h"
int ft_tolower(int c)
{
if (c >= 'A' && c <= 'Z')
return (c + 32);
else
return (c);
}

View file

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_toupper.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/15 13:17:58 by tchampio #+# #+# */
/* Updated: 2024/10/15 13:27:35 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/libft.h"
int ft_toupper(int c)
{
if (c >= 'a' && c <= 'z')
return (c - 32);
else
return (c);
}

25
mlx/LICENSE Normal file
View file

@ -0,0 +1,25 @@
BSD 2-Clause License
Copyright (c) 2021, Ecole 42
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

22
mlx/Makefile Normal file
View file

@ -0,0 +1,22 @@
##
## Makefile for MiniLibX in /home/boulon/work/c/raytraceur/minilibx
##
## Made by Olivier Crouzet
## Login <ol@epitech.net>
##
## Started on Tue Oct 5 15:56:43 2004 Olivier Crouzet
## Last update Tue May 15 15:44:41 2007 Olivier Crouzet
##
## Please use configure script
all : do_configure
do_configure :
./configure
clean :
./configure clean
re : clean all

66
mlx/Makefile.mk Normal file
View file

@ -0,0 +1,66 @@
##
## Makefile for MiniLibX in /home/boulon/work/c/raytraceur/minilibx
##
## Made by Olivier Crouzet
## Login <ol@epitech.net>
##
## Started on Tue Oct 5 15:56:43 2004 Olivier Crouzet
## Last update Tue May 15 15:41:20 2007 Olivier Crouzet
##
## Please use configure script
INC =%%%%
UNAME = $(shell uname)
CC = gcc
ifeq ($(UNAME),FreeBSD)
CC = clang
endif
NAME = libmlx.a
NAME_UNAME = libmlx_$(UNAME).a
SRC = mlx_init.c mlx_new_window.c mlx_pixel_put.c mlx_loop.c \
mlx_mouse_hook.c mlx_key_hook.c mlx_expose_hook.c mlx_loop_hook.c \
mlx_int_anti_resize_win.c mlx_int_do_nothing.c \
mlx_int_wait_first_expose.c mlx_int_get_visual.c \
mlx_flush_event.c mlx_string_put.c mlx_set_font.c \
mlx_new_image.c mlx_get_data_addr.c \
mlx_put_image_to_window.c mlx_get_color_value.c mlx_clear_window.c \
mlx_xpm.c mlx_int_str_to_wordtab.c mlx_destroy_window.c \
mlx_int_param_event.c mlx_int_set_win_event_mask.c mlx_hook.c \
mlx_rgb.c mlx_destroy_image.c mlx_mouse.c mlx_screen_size.c \
mlx_destroy_display.c
OBJ_DIR = obj
OBJ = $(addprefix $(OBJ_DIR)/,$(SRC:%.c=%.o))
CFLAGS = -O3 -I$(INC)
all : $(NAME)
$(OBJ_DIR)/%.o: %.c
@mkdir -p $(OBJ_DIR)
$(CC) $(CFLAGS) $(IFLAGS) -c $< -o $@
$(NAME) : $(OBJ)
ar -r $(NAME) $(OBJ)
ranlib $(NAME)
cp $(NAME) $(NAME_UNAME)
check: all
@test/run_tests.sh
show:
@printf "NAME : $(NAME)\n"
@printf "NAME_UNAME : $(NAME_UNAME)\n"
@printf "CC : $(CC)\n"
@printf "CFLAGS : $(CFLAGS)\n"
@printf "SRC :\n $(SRC)\n"
@printf "OBJ :\n $(OBJ)\n"
clean :
rm -rf $(OBJ_DIR)/ $(NAME) $(NAME_UNAME) *~ core *.core
.PHONY: all check show clean

55
mlx/README.md Executable file
View file

@ -0,0 +1,55 @@
[![Build](https://github.com/42Paris/minilibx-linux/actions/workflows/ci.yml/badge.svg)](https://github.com/42Paris/minilibx-linux/actions/workflows/ci.yml)
This is the MinilibX, a simple X-Window (X11R6) programming API
in C, designed for students, suitable for X-beginners.
Contents
- source code in C to create the mlx library
- man pages (in man/ directory)
- a test program (in test/ directory) is built
with the library
- a public include file mlx.h
- a tiny configure script to generate an appropriate Makefile.gen
Requirements for Linux
- MinilibX only support TrueColor visual type (8,15,16,24 or 32 bits depth)
- gcc
- make
- X11 include files (package xorg)
- XShm extension must be present (package libxext-dev)
- Utility functions from BSD systems - development files (package libbsd-dev)
- **e.g. _sudo apt-get install gcc make xorg libxext-dev libbsd-dev_ (Debian/Ubuntu)**
Requirements for MacOS
- [Xquartz](https://www.xquartz.org/)
```bash
➜ ~ Brew install Xquartz
➜ ~ reboot
➜ ~ xeyes # run an hello world X11 app
```
MlX Color Opacity / Transparency / Alpha (32 bits depth)
- 0xFF (fully transparent) or 0x00 (fully opaque)
Compile MinilibX
- run ./configure or make
both will make a few tests, create Makefile.gen
and then automatically run make on this generated Makefile.gen .
libmlx.a and libmlx_$(HOSTTYPE).a are created.
test/mlx-test binary is also created.
Install MinilibX
- no installation script is provided. You may want to install
- libmlx.a and/or libmlx_$(HOSTTYPE).a in /usr/X11/lib or /usr/local/lib
- mlx.h in /usr/X11/include or /usr/local/include
- man/man3/mlx*.1 in /usr/X11/man/man3 or /usr/local/man/man3
Olivier CROUZET - 2014-01-06 -

126
mlx/configure vendored Executable file
View file

@ -0,0 +1,126 @@
#!/usr/bin/env sh
set -e
BOLD="\033[1m"
RESET="\033[0m"
LIGHT_RED="\033[91m"
LIGHT_GREEN="\033[92m"
LIGHT_CYAN="\033[96m"
logging(){
local type=$1; shift
printf "${LIGHT_CYAN}${BOLD}configure${RESET} [%b] : %b\n" "$type" "$*"
}
log_info(){
logging "${LIGHT_GREEN}info${RESET}" "$@"
}
log_error(){
logging "${LIGHT_RED}error${RESET}" "$@" >&2
}
# find and print x11 header path
get_xlib_include_path(){
local result=""
for inc in \
/usr/X11/include \
/usr/X11R6/include \
/usr/X11R5/include \
/usr/X11R4/include \
\
/usr/include \
/usr/include/X11 \
/usr/include/X11R6 \
/usr/include/X11R5 \
/usr/include/X11R4 \
\
/usr/local/X11/include \
/usr/local/X11R6/include \
/usr/local/X11R5/include \
/usr/local/X11R4/include \
\
/usr/local/include/X11 \
/usr/local/include/X11R6 \
/usr/local/include/X11R5 \
/usr/local/include/X11R4 \
\
/usr/X386/include \
/usr/x386/include \
/usr/XFree86/include/X11 \
\
/usr/local/include \
/usr/athena/include \
/usr/local/x11r5/include \
/usr/lpp/Xamples/include \
\
/usr/openwin/include \
/usr/openwin/share/include
do
if [ -f "$inc/X11/Xlib.h" -a -f "$inc/X11/extensions/XShm.h" ]; then
result=$inc
break
fi
done
echo $result
}
show_help(){
cat <<EOF
Usage :
$0 Auto-configure and make MinilibX
$0 clean Execute the clean rule of both Makefile.gen
EOF
}
clean(){
log_info 'Execute "make clean" from "makefile.gen"'
${MAKE} -f Makefile.gen clean
log_info 'Execute "make clean" from "test/makefile.gen"'
${MAKE} -f Makefile.gen -C test/ --no-print-directory clean
}
parse_args(){
case "$1" in
--help | -h)
show_help
exit 0;;
clean)
clean
exit 0;;
"") return;;
*)
log_error "unknown command \"$1\"\nRun \"./configure --help\" for usage."
exit 1;;
esac
}
main(){
local xlib_inc="$(get_xlib_include_path)"
case $(uname) in
FreeBSD) MAKE=gmake ;;
*) MAKE=make ;;
esac
parse_args "$@"
if [ -z "$xlib_inc" ]; then
log_error "Can't find a suitable X11 include directory."
exit 1
fi
log_info "Found X11 include path directory: $xlib_inc"
log_info 'Generate "makefile.gen" from template "makefile.mk"'
echo "INC=$xlib_inc" > Makefile.gen
cat Makefile.mk | grep -v %%%% >> Makefile.gen
log_info 'Generate "test/makefile.gen" from template "test/makefile.mk"'
echo "INC=$xlib_inc" > test/Makefile.gen
cat test/Makefile.mk | grep -v %%%% >> test/Makefile.gen
log_info 'Execute "make all" from file "makefile.gen"'
${MAKE} -f Makefile.gen all
log_info 'Execute "make all" from file "test/makefile.gen"'
(cd test ; ${MAKE} -f Makefile.gen all )
}
main "$@"

93
mlx/man/man1/mlx.1 Normal file
View file

@ -0,0 +1,93 @@
.TH MiniLibX 3 "September 19, 2002"
.SH NAME
MiniLibX - Simple X-Window Interface Library for students
.SH SYNOPSYS
#include <mlx.h>
.nf
.I void *
.fi
.B mlx_init
();
.SH DESCRIPTION
MiniLibX is an easy way to create graphical software,
without any X-Window programming knowledge. It provides
simple window creation, a drawing tool, image and basic events
management.
.SH X-WINDOW CONCEPT
X-Window is a network-oriented graphical system for Unix.
It is based on two main parts:
.br
On one side, your software wants to draw something on the screen and/or
get keyboard & mouse entries.
.br
On the other side, the X-Server manages the screen, keyboard and mouse
(It is often refered to as a "display").
.br
A network connection must be established between these two entities to send
drawing orders (from the software to the X-Server), and keyboard/mouse
events (from the X-Server to the software).
.SH INCLUDE FILE
.B mlx.h
should be included for a correct use of the MiniLibX API.
It only contains function prototypes, no structure is needed.
.SH LIBRARY FUNCTIONS
.P
First of all, you need to initialize the connection
between your software and the display.
Once this connection is established, you'll be able to
use other MiniLibX functions to send the X-Server messages,
like "I want to draw a yellow pixel in this window" or "did the
user hit a key?".
.P
The
.B mlx_init
function will create this connection. No parameters are needed, ant it will
return a
.I "void *"
identifier, used for further calls to the library routines.
.P
All other MiniLibX functions are described in the following man pages:
.TP 20
.B mlx_new_window
: manage windows
.TP 20
.B mlx_pixel_put
: draw inside window
.TP 20
.B mlx_new_image
: manipulate images
.TP 20
.B mlx_loop
: handle keyboard or mouse events
.SH LINKING MiniLibX
To use MiniLibX functions, you'll need to link
your software with several libraries, including the MiniLibX library itself.
To do this, simply add the following arguments at linking time:
.B -lmlx -lXext -lX11
You may also need to specify the path to these libraries, using
the
.B -L
flag.
.SH RETURN VALUES
If
.B mlx_init()
fails to set up the connection to the X server, it will return NULL, otherwise
a non-null pointer is returned as a connection identifier.
.SH SEE ALSO
mlx_new_window(3), mlx_pixel_put(3), mlx_new_image(3), mlx_loop(3)
.SH AUTHOR
Copyright ol@ - 2002-2014 - Olivier Crouzet

141
mlx/man/man1/mlx_loop.1 Normal file
View file

@ -0,0 +1,141 @@
.TH MiniLibX 3 "September 19, 2002"
.SH NAME
MiniLibX - Handle events
.SH SYNOPSYS
.nf
.I int
.fi
.B mlx_loop
(
.I void *mlx_ptr
);
.nf
.I int
.fi
.B mlx_key_hook
(
.I void *win_ptr, int (*funct_ptr)(), void *param
);
.nf
.I int
.fi
.B mlx_mouse_hook
(
.I void *win_ptr, int (*funct_ptr)(), void *param
);
.nf
.I int
.fi
.B mlx_expose_hook
(
.I void *win_ptr, int (*funct_ptr)(), void *param
);
.nf
.I int
.fi
.B mlx_loop_hook
(
.I void *mlx_ptr, int (*funct_ptr)(), void *param
);
.SH X-WINDOW EVENTS
The X-Window system is bi-directionnal. On one hand, the program sends orders to
the screen to display pixels, images, and so on. On the other hand,
it can get information from the keyboard and mouse associated to
the screen. To do so, the program receives "events" from the keyboard or the
mouse.
.SH DESCRIPTION
To receive events, you must use
.B mlx_loop
(). This function never returns. It is an infinite loop that waits for
an event, and then calls a user-defined function associated with this event.
A single parameter is needed, the connection identifier
.I mlx_ptr
(see the
.B mlx manual).
You can assign different functions to the three following events:
.br
- A key is pressed
.br
- The mouse button is pressed
.br
- A part of the window should be re-drawn
(this is called an "expose" event, and it is your program's job to handle it).
.br
Each window can define a different function for the same event.
The three functions
.B mlx_key_hook
(),
.B mlx_mouse_hook
() and
.B mlx_expose_hook
() work exactly the same way.
.I funct_ptr
is a pointer to the function you want to be called
when an event occurs. This assignment is specific to the window defined by the
.I win_ptr
identifier. The
.I param
adress will be passed to the function everytime it is called, and should be
used to store the parameters it might need.
The syntax for the
.B mlx_loop_hook
() function is identical to the previous ones, but the given function will be
called when no event occurs.
When it catches an event, the MiniLibX calls the corresponding function
with fixed parameters:
.nf
expose_hook(void *param);
key_hook(int keycode,void *param);
mouse_hook(int button,int x,int y,void *param);
loop_hook(void *param);
.fi
These function names are arbitrary. They here are used to distinguish
parameters according to the event. These functions are NOT part of the
MiniLibX.
.I param
is the address specified in the mlx_*_hook calls. This address is never
used nor modified by the MiniLibX. On key and mouse events, additional
information is passed:
.I keycode
tells you which key is pressed (look for the X11 include file "keysymdef.h"),
(
.I x
,
.I y
) are the coordinates of the mouse click in the window, and
.I button
tells you which mouse button was pressed.
.SH GOING FURTHER WITH EVENTS
The MiniLibX provides a much generic access to all X-Window events. The
.I mlx.h
include define
.B mlx_hook()
in the same manner mlx_*_hook functions work. The event and mask values
will be taken from the X11 include file "X.h".
See source code of mlx_int_param_event.c to find out how the MiniLibX will
call your own function for a specific event.
.SH SEE ALSO
mlx(3), mlx_new_window(3), mlx_pixel_put(3), mlx_new_image(3)
.SH AUTHOR
Copyright ol@ - 2002-2014 - Olivier Crouzet

View file

@ -0,0 +1,192 @@
.TH MiniLibX 3 "September 19, 2002"
.SH NAME
MiniLibX - Manipulating images
.SH SYNOPSYS
.nf
.I void *
.fi
.B mlx_new_image
(
.I void *mlx_ptr, int width, int height
);
.nf
.I char *
.fi
.B mlx_get_data_addr
(
.I void *img_ptr, int *bits_per_pixel, int *size_line, int *endian
);
.nf
.I int
.fi
.B mlx_put_image_to_window
(
.I void *mlx_ptr, void *win_ptr, void *img_ptr, int x, int y
);
.nf
.I unsigned int
.fi
.B mlx_get_color_value
(
.I void *mlx_ptr, int color
);
.nf
.I void *
.fi
.B mlx_xpm_to_image
(
.I void *mlx_ptr, char **xpm_data, int *width, int *height
);
.nf
.I void *
.fi
.B mlx_xpm_file_to_image
(
.I void *mlx_ptr, char *filename, int *width, int *height
);
.nf
.I int
.fi
.B mlx_destroy_image
(
.I void *mlx_ptr, void *img_ptr
);
.SH DESCRIPTION
.B mlx_new_image
() creates a new image in memory. It returns a
.I void *
identifier needed to manipulate this image later. It only needs
the size of the image to be created, using the
.I width
and
.I height
parameters, and the
.I mlx_ptr
connection identifier (see the
.B mlx
manual).
The user can draw inside the image (see below), and
can dump the image inside a specified window at any time to
display it on the screen. This is done using
.B mlx_put_image_to_window
(). Three identifiers are needed here, for the connection to the
display, the window to use, and the image (respectively
.I mlx_ptr
,
.I win_ptr
and
.I img_ptr
). The (
.I x
,
.I y
) coordinates define where the image should be placed in the window.
.B mlx_get_data_addr
() returns information about the created image, allowing a user
to modify it later. The
.I img_ptr
parameter specifies the image to use. The three next parameters should
be the addresses of three different valid integers.
.I bits_per_pixel
will be filled with the number of bits needed to represent a pixel color
(also called the depth of the image).
.I size_line
is the number of bytes used to store one line of the image in memory.
This information is needed to move from one line to another in the image.
.I endian
tells you wether the pixel color in the image needs to be stored in
little endian (
.I endian
== 0), or big endian (
.I endian
== 1).
.B mlx_get_data_addr
returns a
.I char *
address that represents the begining of the memory area where the image
is stored. From this adress, the first
.I bits_per_pixel
bits represent the color of the first pixel in the first line of
the image. The second group of
.I bits_per_pixel
bits represent the second pixel of the first line, and so on.
Add
.I size_line
to the adress to get the begining of the second line. You can reach any
pixels of the image that way.
.B mlx_destroy_image
destroys the given image (
.I img_ptr
).
.SH STORING COLOR INSIDE IMAGES
Depending on the display, the number of bits used to store a pixel color
can change. The user usually represents a color in RGB mode, using
one byte for each component (see
.B mlx_pixel_put
manual). This must be translated to fit the
.I bits_per_pixel
requirement of the image, and make the color understandable to the X-Server.
That is the purpose of the
.B mlx_get_color_value
() function. It takes a standard RGB
.I color
parameter, and returns an
.I unsigned int
value.
The
.I bits_per_pixel
least significant bits of this value can be stored in the image.
Keep in mind that the least significant bits position depends on the local
computer's endian. If the endian of the image (in fact the endian of
the X-Server's computer) differs from the local endian, then the value should
be transformed before being used.
.SH XPM IMAGES
The
.B mlx_xpm_to_image
() and
.B mlx_xpm_file_to_image
() functions will create a new image the same way.
They will fill it using the specified
.I xpm_data
or
.I filename
, depending on which function is used.
Note that MiniLibX does not use the standard
Xpm library to deal with xpm images. You may not be able to
read all types of xpm images. It however handles transparency.
.SH RETURN VALUES
The three functions that create images,
.B mlx_new_image()
,
.B mlx_xpm_to_image()
and
.B mlx_xpm_file_to_image()
, will return NULL if an error occurs. Otherwise they return a non-null pointer
as an image identifier.
.SH SEE ALSO
mlx(3), mlx_new_window(3), mlx_pixel_put(3), mlx_loop(3)
.SH AUTHOR
Copyright ol@ - 2002-2014 - Olivier Crouzet

View file

@ -0,0 +1,79 @@
.TH MiniLibX 3 "September 19, 2002"
.SH NAME
MiniLibX - Managing windows
.SH SYNOPSYS
.nf
.I void *
.fi
.B mlx_new_window
(
.I void *mlx_ptr, int size_x, int size_y, char *title
);
.nf
.I int
.fi
.B mlx_clear_window
(
.I void *mlx_ptr, void *win_ptr
);
.nf
.I int
.fi
.B mlx_destroy_window
(
.I void *mlx_ptr, void *win_ptr
);
.SH DESCRIPTION
The
.B mlx_new_window
() function creates a new window on the screen, using the
.I size_x
and
.I size_y
parameters to determine its size, and
.I title
as the text that should be displayed in the window's title bar.
The
.I mlx_ptr
parameter is the connection identifier returned by
.B mlx_init
() (see the
.B mlx
man page).
.B mlx_new_window
() returns a
.I void *
window identifier that can be used by other MiniLibX calls.
Note that the MiniLibX
can handle an arbitrary number of separate windows.
.B mlx_clear_window
() and
.B mlx_destroy_window
() respectively clear (in black) and destroy the given window. They both have
the same parameters:
.I mlx_ptr
is the screen connection identifier, and
.I win_ptr
is a window identifier.
.SH RETURN VALUES
If
.B mlx_new_window()
fails to create a new window (for wathever reason), it will return NULL,
otherwise a non-null pointer is returned as a window identifier.
.B mlx_clear_window
and
.B mlx_destroy_window
right now return nothing.
.SH SEE ALSO
mlx(3), mlx_pixel_put(3), mlx_new_image(3), mlx_loop(3)
.SH AUTHOR
Copyright ol@ - 2002-2014 - Olivier Crouzet

View file

@ -0,0 +1,84 @@
.TH MiniLibX 3 "September 19, 2002"
.SH NAME
MiniLibX - Drawing inside windows
.SH SYNOPSYS
.nf
.I int
.fi
.B mlx_pixel_put
(
.I void *mlx_ptr, void *win_ptr, int x, int y, int color
);
.nf
.I int
.fi
.B mlx_string_put
(
.I void *mlx_ptr, void *win_ptr, int x, int y, int color, char *string
);
.SH DESCRIPTION
The
.B mlx_pixel_put
() function draws a defined pixel in the window
.I win_ptr
using the (
.I x
,
.I y
) coordinates, and the specified
.I color
\&. The origin (0,0) is the upper left corner of the window, the x and y axis
respectively pointing right and down. The connection
identifier,
.I mlx_ptr
, is needed (see the
.B mlx
man page).
Parameters for
.B mlx_string_put
() have the same meaning. Instead of a simple pixel, the specified
.I string
will be displayed at (
.I x
,
.I y
).
In both functions, it is impossible to display anything outside the
specified window, nor display in another window in front of the selected one.
.SH COLOR MANAGEMENT
The
.I color
parameter has an integer type. The displayed color needs to be encoded
in this integer, following a defined scheme. All displayable colors
can be split in 3 basic colors: red, green and blue. Three associated
values, in the 0-255 range, represent how much of each color is mixed up
to create the original color. Theses three values must be set inside the
integer to display the right color. The three least significant bytes of
this integer are filled as shown in the picture below:
.TS
allbox;
c s s s s
r c c c c.
Color Integer
Interpretation \[*a] R G B
Bit numbers 31..24 23..16 15..8 7..0
.TE
While filling the integer, make sure you avoid endian problems. Remember
that the "blue" byte should always be the least significant one.
.SH SEE ALSO
mlx(3), mlx_new_window(3), mlx_new_image(3), mlx_loop(3)
.SH AUTHOR
Copyright ol@ - 2002-2014 - Olivier Crouzet

93
mlx/man/man3/mlx.3 Normal file
View file

@ -0,0 +1,93 @@
.TH MiniLibX 3 "September 19, 2002"
.SH NAME
MiniLibX - Simple X-Window Interface Library for students
.SH SYNOPSYS
#include <mlx.h>
.nf
.I void *
.fi
.B mlx_init
();
.SH DESCRIPTION
MiniLibX is an easy way to create graphical software,
without any X-Window programming knowledge. It provides
simple window creation, a drawing tool, image and basic events
management.
.SH X-WINDOW CONCEPT
X-Window is a network-oriented graphical system for Unix.
It is based on two main parts:
.br
On one side, your software wants to draw something on the screen and/or
get keyboard & mouse entries.
.br
On the other side, the X-Server manages the screen, keyboard and mouse
(It is often refered to as a "display").
.br
A network connection must be established between these two entities to send
drawing orders (from the software to the X-Server), and keyboard/mouse
events (from the X-Server to the software).
.SH INCLUDE FILE
.B mlx.h
should be included for a correct use of the MiniLibX API.
It only contains function prototypes, no structure is needed.
.SH LIBRARY FUNCTIONS
.P
First of all, you need to initialize the connection
between your software and the display.
Once this connection is established, you'll be able to
use other MiniLibX functions to send the X-Server messages,
like "I want to draw a yellow pixel in this window" or "did the
user hit a key?".
.P
The
.B mlx_init
function will create this connection. No parameters are needed, ant it will
return a
.I "void *"
identifier, used for further calls to the library routines.
.P
All other MiniLibX functions are described in the following man pages:
.TP 20
.B mlx_new_window
: manage windows
.TP 20
.B mlx_pixel_put
: draw inside window
.TP 20
.B mlx_new_image
: manipulate images
.TP 20
.B mlx_loop
: handle keyboard or mouse events
.SH LINKING MiniLibX
To use MiniLibX functions, you'll need to link
your software with several libraries, including the MiniLibX library itself.
To do this, simply add the following arguments at linking time:
.B -lmlx -lXext -lX11
You may also need to specify the path to these libraries, using
the
.B -L
flag.
.SH RETURN VALUES
If
.B mlx_init()
fails to set up the connection to the X server, it will return NULL, otherwise
a non-null pointer is returned as a connection identifier.
.SH SEE ALSO
mlx_new_window(3), mlx_pixel_put(3), mlx_new_image(3), mlx_loop(3)
.SH AUTHOR
Copyright ol@ - 2002-2014 - Olivier Crouzet

141
mlx/man/man3/mlx_loop.3 Normal file
View file

@ -0,0 +1,141 @@
.TH MiniLibX 3 "September 19, 2002"
.SH NAME
MiniLibX - Handle events
.SH SYNOPSYS
.nf
.I int
.fi
.B mlx_loop
(
.I void *mlx_ptr
);
.nf
.I int
.fi
.B mlx_key_hook
(
.I void *win_ptr, int (*funct_ptr)(), void *param
);
.nf
.I int
.fi
.B mlx_mouse_hook
(
.I void *win_ptr, int (*funct_ptr)(), void *param
);
.nf
.I int
.fi
.B mlx_expose_hook
(
.I void *win_ptr, int (*funct_ptr)(), void *param
);
.nf
.I int
.fi
.B mlx_loop_hook
(
.I void *mlx_ptr, int (*funct_ptr)(), void *param
);
.SH X-WINDOW EVENTS
The X-Window system is bi-directionnal. On one hand, the program sends orders to
the screen to display pixels, images, and so on. On the other hand,
it can get information from the keyboard and mouse associated to
the screen. To do so, the program receives "events" from the keyboard or the
mouse.
.SH DESCRIPTION
To receive events, you must use
.B mlx_loop
(). This function never returns. It is an infinite loop that waits for
an event, and then calls a user-defined function associated with this event.
A single parameter is needed, the connection identifier
.I mlx_ptr
(see the
.B mlx manual).
You can assign different functions to the three following events:
.br
- A key is pressed
.br
- The mouse button is pressed
.br
- A part of the window should be re-drawn
(this is called an "expose" event, and it is your program's job to handle it).
.br
Each window can define a different function for the same event.
The three functions
.B mlx_key_hook
(),
.B mlx_mouse_hook
() and
.B mlx_expose_hook
() work exactly the same way.
.I funct_ptr
is a pointer to the function you want to be called
when an event occurs. This assignment is specific to the window defined by the
.I win_ptr
identifier. The
.I param
adress will be passed to the function everytime it is called, and should be
used to store the parameters it might need.
The syntax for the
.B mlx_loop_hook
() function is identical to the previous ones, but the given function will be
called when no event occurs.
When it catches an event, the MiniLibX calls the corresponding function
with fixed parameters:
.nf
expose_hook(void *param);
key_hook(int keycode,void *param);
mouse_hook(int button,int x,int y,void *param);
loop_hook(void *param);
.fi
These function names are arbitrary. They here are used to distinguish
parameters according to the event. These functions are NOT part of the
MiniLibX.
.I param
is the address specified in the mlx_*_hook calls. This address is never
used nor modified by the MiniLibX. On key and mouse events, additional
information is passed:
.I keycode
tells you which key is pressed (look for the X11 include file "keysymdef.h"),
(
.I x
,
.I y
) are the coordinates of the mouse click in the window, and
.I button
tells you which mouse button was pressed.
.SH GOING FURTHER WITH EVENTS
The MiniLibX provides a much generic access to all X-Window events. The
.I mlx.h
include define
.B mlx_hook()
in the same manner mlx_*_hook functions work. The event and mask values
will be taken from the X11 include file "X.h".
See source code of mlx_int_param_event.c to find out how the MiniLibX will
call your own function for a specific event.
.SH SEE ALSO
mlx(3), mlx_new_window(3), mlx_pixel_put(3), mlx_new_image(3)
.SH AUTHOR
Copyright ol@ - 2002-2014 - Olivier Crouzet

View file

@ -0,0 +1,192 @@
.TH MiniLibX 3 "September 19, 2002"
.SH NAME
MiniLibX - Manipulating images
.SH SYNOPSYS
.nf
.I void *
.fi
.B mlx_new_image
(
.I void *mlx_ptr, int width, int height
);
.nf
.I char *
.fi
.B mlx_get_data_addr
(
.I void *img_ptr, int *bits_per_pixel, int *size_line, int *endian
);
.nf
.I int
.fi
.B mlx_put_image_to_window
(
.I void *mlx_ptr, void *win_ptr, void *img_ptr, int x, int y
);
.nf
.I unsigned int
.fi
.B mlx_get_color_value
(
.I void *mlx_ptr, int color
);
.nf
.I void *
.fi
.B mlx_xpm_to_image
(
.I void *mlx_ptr, char **xpm_data, int *width, int *height
);
.nf
.I void *
.fi
.B mlx_xpm_file_to_image
(
.I void *mlx_ptr, char *filename, int *width, int *height
);
.nf
.I int
.fi
.B mlx_destroy_image
(
.I void *mlx_ptr, void *img_ptr
);
.SH DESCRIPTION
.B mlx_new_image
() creates a new image in memory. It returns a
.I void *
identifier needed to manipulate this image later. It only needs
the size of the image to be created, using the
.I width
and
.I height
parameters, and the
.I mlx_ptr
connection identifier (see the
.B mlx
manual).
The user can draw inside the image (see below), and
can dump the image inside a specified window at any time to
display it on the screen. This is done using
.B mlx_put_image_to_window
(). Three identifiers are needed here, for the connection to the
display, the window to use, and the image (respectively
.I mlx_ptr
,
.I win_ptr
and
.I img_ptr
). The (
.I x
,
.I y
) coordinates define where the image should be placed in the window.
.B mlx_get_data_addr
() returns information about the created image, allowing a user
to modify it later. The
.I img_ptr
parameter specifies the image to use. The three next parameters should
be the addresses of three different valid integers.
.I bits_per_pixel
will be filled with the number of bits needed to represent a pixel color
(also called the depth of the image).
.I size_line
is the number of bytes used to store one line of the image in memory.
This information is needed to move from one line to another in the image.
.I endian
tells you wether the pixel color in the image needs to be stored in
little endian (
.I endian
== 0), or big endian (
.I endian
== 1).
.B mlx_get_data_addr
returns a
.I char *
address that represents the begining of the memory area where the image
is stored. From this adress, the first
.I bits_per_pixel
bits represent the color of the first pixel in the first line of
the image. The second group of
.I bits_per_pixel
bits represent the second pixel of the first line, and so on.
Add
.I size_line
to the adress to get the begining of the second line. You can reach any
pixels of the image that way.
.B mlx_destroy_image
destroys the given image (
.I img_ptr
).
.SH STORING COLOR INSIDE IMAGES
Depending on the display, the number of bits used to store a pixel color
can change. The user usually represents a color in RGB mode, using
one byte for each component (see
.B mlx_pixel_put
manual). This must be translated to fit the
.I bits_per_pixel
requirement of the image, and make the color understandable to the X-Server.
That is the purpose of the
.B mlx_get_color_value
() function. It takes a standard RGB
.I color
parameter, and returns an
.I unsigned int
value.
The
.I bits_per_pixel
least significant bits of this value can be stored in the image.
Keep in mind that the least significant bits position depends on the local
computer's endian. If the endian of the image (in fact the endian of
the X-Server's computer) differs from the local endian, then the value should
be transformed before being used.
.SH XPM IMAGES
The
.B mlx_xpm_to_image
() and
.B mlx_xpm_file_to_image
() functions will create a new image the same way.
They will fill it using the specified
.I xpm_data
or
.I filename
, depending on which function is used.
Note that MiniLibX does not use the standard
Xpm library to deal with xpm images. You may not be able to
read all types of xpm images. It however handles transparency.
.SH RETURN VALUES
The three functions that create images,
.B mlx_new_image()
,
.B mlx_xpm_to_image()
and
.B mlx_xpm_file_to_image()
, will return NULL if an error occurs. Otherwise they return a non-null pointer
as an image identifier.
.SH SEE ALSO
mlx(3), mlx_new_window(3), mlx_pixel_put(3), mlx_loop(3)
.SH AUTHOR
Copyright ol@ - 2002-2014 - Olivier Crouzet

View file

@ -0,0 +1,79 @@
.TH MiniLibX 3 "September 19, 2002"
.SH NAME
MiniLibX - Managing windows
.SH SYNOPSYS
.nf
.I void *
.fi
.B mlx_new_window
(
.I void *mlx_ptr, int size_x, int size_y, char *title
);
.nf
.I int
.fi
.B mlx_clear_window
(
.I void *mlx_ptr, void *win_ptr
);
.nf
.I int
.fi
.B mlx_destroy_window
(
.I void *mlx_ptr, void *win_ptr
);
.SH DESCRIPTION
The
.B mlx_new_window
() function creates a new window on the screen, using the
.I size_x
and
.I size_y
parameters to determine its size, and
.I title
as the text that should be displayed in the window's title bar.
The
.I mlx_ptr
parameter is the connection identifier returned by
.B mlx_init
() (see the
.B mlx
man page).
.B mlx_new_window
() returns a
.I void *
window identifier that can be used by other MiniLibX calls.
Note that the MiniLibX
can handle an arbitrary number of separate windows.
.B mlx_clear_window
() and
.B mlx_destroy_window
() respectively clear (in black) and destroy the given window. They both have
the same parameters:
.I mlx_ptr
is the screen connection identifier, and
.I win_ptr
is a window identifier.
.SH RETURN VALUES
If
.B mlx_new_window()
fails to create a new window (for wathever reason), it will return NULL,
otherwise a non-null pointer is returned as a window identifier.
.B mlx_clear_window
and
.B mlx_destroy_window
right now return nothing.
.SH SEE ALSO
mlx(3), mlx_pixel_put(3), mlx_new_image(3), mlx_loop(3)
.SH AUTHOR
Copyright ol@ - 2002-2014 - Olivier Crouzet

View file

@ -0,0 +1,81 @@
.TH MiniLibX 3 "September 19, 2002"
.SH NAME
MiniLibX - Drawing inside windows
.SH SYNOPSYS
.nf
.I int
.fi
.B mlx_pixel_put
(
.I void *mlx_ptr, void *win_ptr, int x, int y, int color
);
.nf
.I int
.fi
.B mlx_string_put
(
.I void *mlx_ptr, void *win_ptr, int x, int y, int color, char *string
);
.SH DESCRIPTION
The
.B mlx_pixel_put
() function draws a defined pixel in the window
.I win_ptr
using the (
.I x
,
.I y
) coordinates, and the specified
.I color
\&. The origin (0,0) is the upper left corner of the window, the x and y axis
respectively pointing right and down. The connection
identifier,
.I mlx_ptr
, is needed (see the
.B mlx
man page).
Parameters for
.B mlx_string_put
() have the same meaning. Instead of a simple pixel, the specified
.I string
will be displayed at (
.I x
,
.I y
).
In both functions, it is impossible to display anything outside the
specified window, nor display in another window in front of the selected one.
.SH COLOR MANAGEMENT
The
.I color
parameter has an integer type. The displayed color needs to be encoded
in this integer, following a defined scheme. All displayable colors
can be split in 3 basic colors: red, green and blue. Three associated
values, in the 0-255 range, represent how much of each color is mixed up
to create the original color. Theses three values must be set inside the
integer to display the right color. The three least significant bytes of
this integer are filled as shown in the picture below:
.nf
| 0 | R | G | B | color integer
+---+---+---+---+
.fi
While filling the integer, make sure you avoid endian problems. Remember
that the "blue" byte should always be the least significant one.
.SH SEE ALSO
mlx(3), mlx_new_window(3), mlx_new_image(3), mlx_loop(3)
.SH AUTHOR
Copyright ol@ - 2002-2014 - Olivier Crouzet

67
mlx/minilibx-linux/.gitignore vendored Normal file
View file

@ -0,0 +1,67 @@
## Mlx related
Makefile.gen
/test/mlx-test
## Editor
.vscode/*
*~
\#*\#
## Other
.DS_STORE
## Template from https://github.com/github/gitignore
# Prerequisites
*.d
# Object files
*.o
*.ko
*.obj
*.elf
# Linker output
*.ilk
*.map
*.exp
# Precompiled Headers
*.gch
*.pch
# Libraries
*.lib
*.a
*.la
*.lo
# Shared objects (inc. Windows DLLs)
*.dll
*.so
*.so.*
*.dylib
# Executables
*.exe
*.out
*.app
*.i*86
*.x86_64
*.hex
# Debug files
*.dSYM/
*.su
*.idb
*.pdb
# Kernel Module Compile Results
*.mod*
*.cmd
.tmp_versions/
modules.order
Module.symvers
Mkfile.old
dkms.conf

139
mlx/mlx.h Normal file
View file

@ -0,0 +1,139 @@
/*
** mlx.h for MinilibX in
**
** Made by Charlie Root
** Login <ol@epitech.net>
**
** Started on Mon Jul 31 16:37:50 2000 Charlie Root
** Last update Tue May 15 16:23:28 2007 Olivier Crouzet
*/
/*
** MinilibX - Please report bugs
*/
/*
** FR msg - FR msg - FR msg
**
** La MinilibX utilise 2 librairies supplementaires qu'il
** est necessaire de rajouter a la compilation :
** -lmlx -lXext -lX11
**
** La MinilibX permet le chargement des images de type Xpm.
** Notez que cette implementation est incomplete.
** Merci de communiquer tout probleme de chargement d'image
** de ce type.
*/
#ifndef MLX_H
#define MLX_H
void *mlx_init();
/*
** needed before everything else.
** return (void *)0 if failed
*/
/*
** Basic actions
*/
void *mlx_new_window(void *mlx_ptr, int size_x, int size_y, char *title);
/*
** return void *0 if failed
*/
int mlx_clear_window(void *mlx_ptr, void *win_ptr);
int mlx_pixel_put(void *mlx_ptr, void *win_ptr, int x, int y, int color);
/*
** origin for x & y is top left corner of the window
** y down is positive
** color is 0x00RRGGBB
*/
/*
** Image stuff
*/
void *mlx_new_image(void *mlx_ptr,int width,int height);
/*
** return void *0 if failed
** obsolete : image2 data is stored using bit planes
** void *mlx_new_image2(void *mlx_ptr,int width,int height);
*/
char *mlx_get_data_addr(void *img_ptr, int *bits_per_pixel,
int *size_line, int *endian);
/*
** endian : 0 = sever X is little endian, 1 = big endian
** for mlx_new_image2, 2nd arg of mlx_get_data_addr is number_of_planes
*/
int mlx_put_image_to_window(void *mlx_ptr, void *win_ptr, void *img_ptr,
int x, int y);
int mlx_get_color_value(void *mlx_ptr, int color);
/*
** dealing with Events
*/
int mlx_mouse_hook (void *win_ptr, int (*funct_ptr)(), void *param);
int mlx_key_hook (void *win_ptr, int (*funct_ptr)(), void *param);
int mlx_expose_hook (void *win_ptr, int (*funct_ptr)(), void *param);
int mlx_loop_hook (void *mlx_ptr, int (*funct_ptr)(), void *param);
int mlx_loop (void *mlx_ptr);
int mlx_loop_end (void *mlx_ptr);
/*
** hook funct are called as follow :
**
** expose_hook(void *param);
** key_hook(int keycode, void *param);
** mouse_hook(int button, int x,int y, void *param);
** loop_hook(void *param);
**
*/
/*
** Usually asked...
*/
int mlx_string_put(void *mlx_ptr, void *win_ptr, int x, int y, int color,
char *string);
void mlx_set_font(void *mlx_ptr, void *win_ptr, char *name);
void *mlx_xpm_to_image(void *mlx_ptr, char **xpm_data,
int *width, int *height);
void *mlx_xpm_file_to_image(void *mlx_ptr, char *filename,
int *width, int *height);
int mlx_destroy_window(void *mlx_ptr, void *win_ptr);
int mlx_destroy_image(void *mlx_ptr, void *img_ptr);
int mlx_destroy_display(void *mlx_ptr);
/*
** generic hook system for all events, and minilibX functions that
** can be hooked. Some macro and defines from X11/X.h are needed here.
*/
int mlx_hook(void *win_ptr, int x_event, int x_mask,
int (*funct)(), void *param);
int mlx_do_key_autorepeatoff(void *mlx_ptr);
int mlx_do_key_autorepeaton(void *mlx_ptr);
int mlx_do_sync(void *mlx_ptr);
int mlx_mouse_get_pos(void *mlx_ptr, void *win_ptr, int *x, int *y);
int mlx_mouse_move(void *mlx_ptr, void *win_ptr, int x, int y);
int mlx_mouse_hide(void *mlx_ptr, void *win_ptr);
int mlx_mouse_show(void *mlx_ptr, void *win_ptr);
int mlx_get_screen_size(void *mlx_ptr, int *sizex, int *sizey);
#endif /* MLX_H */

21
mlx/mlx_clear_window.c Normal file
View file

@ -0,0 +1,21 @@
/*
** mlx_clear_window.c for MiniLibX in
**
** Made by Charlie Root
** Login <ol@epitech.net>
**
** Started on Thu Sep 7 19:46:15 2000 Charlie Root
** Last update Tue Sep 25 17:11:19 2001 Charlie Root
*/
#include "mlx_int.h"
int mlx_clear_window(t_xvar *xvar,t_win_list *win)
{
XClearWindow(xvar->display,win->window);
if (xvar->do_flush)
XFlush(xvar->display);
}

18
mlx/mlx_destroy_display.c Normal file
View file

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* mlx_destroy_display.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mg <mg@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/10/03 18:56:35 by mg #+# #+# */
/* Updated: 2020/10/04 01:55:35 by mg ### ########.fr */
/* */
/* ************************************************************************** */
#include "mlx_int.h"
int mlx_destroy_display(t_xvar *xvar)
{
XCloseDisplay(xvar->display);
}

31
mlx/mlx_destroy_image.c Normal file
View file

@ -0,0 +1,31 @@
/*
** mlx_destroy_image.c for MinilibX in
**
** Made by Charlie Root
** Login <ol@epitech.net>
**
** Started on Tue Mar 12 10:25:15 2002 Charlie Root
** Last update Tue May 15 16:45:54 2007 Olivier Crouzet
*/
#include "mlx_int.h"
int mlx_destroy_image(t_xvar *xvar, t_img *img)
{
if (img->type == MLX_TYPE_SHM_PIXMAP ||
img->type == MLX_TYPE_SHM)
{
XShmDetach(xvar->display, &(img->shm));
shmdt(img->shm.shmaddr);
/* shmctl IPC_RMID already done */
}
XDestroyImage(img->image); /* For image & shm-image. Also free img->data */
XFreePixmap(xvar->display, img->pix);
if (img->gc)
XFreeGC(xvar->display, img->gc);
free(img);
if (xvar->do_flush)
XFlush(xvar->display);
}

38
mlx/mlx_destroy_window.c Normal file
View file

@ -0,0 +1,38 @@
/*
** mlx_destroy_window.c for MinilibX in
**
** Made by Charlie Root
** Login <ol@epitech.net>
**
** Started on Tue Mar 12 10:25:15 2002 Charlie Root
** Last update Tue May 15 16:46:08 2007 Olivier Crouzet
*/
#include "mlx_int.h"
int mlx_destroy_window(t_xvar *xvar,t_win_list *win)
{
t_win_list *w;
t_win_list *prev;
t_win_list first;
first.next = xvar->win_list;
prev = &first;
w = prev->next;
while (w)
{
if (w==win)
prev->next = w->next;
else
prev = w;
w = w->next;
}
xvar->win_list = first.next;
XDestroyWindow(xvar->display,win->window);
XFreeGC(xvar->display,win->gc);
free(win);
if (xvar->do_flush)
XFlush(xvar->display);
}

22
mlx/mlx_expose_hook.c Normal file
View file

@ -0,0 +1,22 @@
/*
** mlx_expose_hook.c for MiniLibX in
**
** Made by Charlie Root
** Login <ol@epitech.net>
**
** Started on Thu Aug 3 11:49:06 2000 Charlie Root
** Last update Fri Feb 23 17:07:42 2001 Charlie Root
*/
#include "mlx_int.h"
int mlx_expose_hook(t_win_list *win,int (*funct)(),void *param)
{
win->hooks[Expose].hook = funct;
win->hooks[Expose].param = param;
win->hooks[Expose].mask = ExposureMask;
}

104
mlx/mlx_ext_randr.c Normal file
View file

@ -0,0 +1,104 @@
#include "mlx_int.h"
#include <unistd.h>
#include <X11/extensions/Xrandr.h>
/* global for independant extension */
RRMode saved_mode = 0;
int mlx_ext_fullscreen(t_xvar *xvar, t_win_list *win, int fullscreen)
{
XWindowAttributes watt;
int i;
int j;
XRRScreenResources *res;
XRROutputInfo *o_info;
XRRCrtcInfo *crtc;
RRMode mode_candidate;
int idx_output;
int idx_candidate;
if (!XGetWindowAttributes(xvar->display, win->window, &watt))
return (0);
res = XRRGetScreenResources(xvar->display, xvar->root);
o_info = NULL;
idx_output = -1;
i = res->noutput;
while (i--)
{
o_info = XRRGetOutputInfo(xvar->display, res, res->outputs[i]);
if (o_info->connection == RR_Connected)
{
idx_output = i;
i = 0;
}
else
XRRFreeOutputInfo(o_info);
}
if (!o_info)
{
XRRFreeScreenResources(res);
return (0);
}
idx_candidate = -1;
i = o_info->nmode;
while (i--)
{
j = res->nmode;
while (j--)
if (res->modes[j].id == o_info->modes[i])
if (res->modes[j].width >= watt.width && res->modes[j].height >= watt.height &&
(idx_candidate == -1 || res->modes[idx_candidate].width > res->modes[j].width ||
res->modes[idx_candidate].height > res->modes[j].height) )
idx_candidate = i;
}
if (idx_candidate < 0)
{
XRRFreeOutputInfo(o_info);
XRRFreeScreenResources(res);
return (0);
}
if (!fullscreen && saved_mode == -1)
idx_candidate = 0; /* if no clue, uses first mode, usually part of npreferred */
mode_candidate = o_info->modes[idx_candidate];
if (!fullscreen)
mode_candidate = saved_mode;
crtc = XRRGetCrtcInfo(xvar->display, res, o_info->crtc);
saved_mode = crtc->mode;
i = XRRSetCrtcConfig(xvar->display, res, o_info->crtc, CurrentTime, 0, 0, mode_candidate,
crtc->rotation, &res->outputs[idx_output], 1);
if (fullscreen)
printf("found mode : %d x %d\n Status %d\n", res->modes[idx_candidate].width, res->modes[idx_candidate].height, i);
else
printf("back previous mode\n");
XMoveWindow(xvar->display, win->window, 0, 0);
XMapRaised(xvar->display, win->window);
if (fullscreen)
{
// XGrabPointer(xvar->display, win->window, True, 0, GrabModeAsync, GrabModeAsync, win->window, 0L, CurrentTime);
XGrabKeyboard(xvar->display, win->window, False, GrabModeAsync, GrabModeAsync, CurrentTime);
}
else
{
XUngrabPointer(xvar->display, CurrentTime);
XUngrabKeyboard(xvar->display, CurrentTime);
}
XSync(xvar->display, False);
sleep(1);
XRRFreeCrtcInfo(crtc);
XRRFreeOutputInfo(o_info);
XRRFreeScreenResources(res);
}

25
mlx/mlx_flush_event.c Normal file
View file

@ -0,0 +1,25 @@
/*
** mlx_flush_event.c for MiniLibX in
**
** Made by Charlie Root
** Login <ol@epitech.net>
**
** Started on Wed Aug 2 18:58:11 2000 Charlie Root
** Last update Fri Feb 23 17:08:48 2001 Charlie Root
*/
#include "mlx_int.h"
int mlx_flush_event(t_xvar *xvar)
{
XEvent ev;
while (XPending(xvar->display))
{
XNextEvent(xvar->display,&ev);
}
}

33
mlx/mlx_get_color_value.c Normal file
View file

@ -0,0 +1,33 @@
/*
** mlx_get_color_value.c for MiniLibX in
**
** Made by Charlie Root
** Login <ol@epitech.net>
**
** Started on Mon Jul 31 19:01:33 2000 Charlie Root
** Last update Thu Oct 4 15:04:13 2001 Charlie Root
*/
#include "mlx_int.h"
int mlx_get_color_value(t_xvar *xvar,int color)
{
return(mlx_int_get_good_color(xvar,color));
}
int mlx_int_get_good_color(t_xvar *xvar,int color)
{
XColor xc;
if (xvar->depth>=24)
return (color);
xc.red = (color>>8)&0xFF00;
xc.green = color&0xFF00;
xc.blue = (color<<8)&0xFF00;
xc.pixel = ((xc.red>>(16-xvar->decrgb[1]))<<xvar->decrgb[0])+
((xc.green>>(16-xvar->decrgb[3]))<<xvar->decrgb[2])+
((xc.blue>>(16-xvar->decrgb[5]))<<xvar->decrgb[4]);
return (xc.pixel);
}

23
mlx/mlx_get_data_addr.c Normal file
View file

@ -0,0 +1,23 @@
/*
** mlx_get_data_addr.c for MiniLibX in raytraceur
**
** Made by Charlie Root
** Login <ol@epitech.net>
**
** Started on Mon Aug 14 15:45:57 2000 Charlie Root
** Last update Thu Sep 27 19:05:25 2001 Charlie Root
*/
#include "mlx_int.h"
char *mlx_get_data_addr(t_img *img,int *bits_per_pixel,
int *size_line,int *endian)
{
*bits_per_pixel = img->bpp;
*size_line = img->size_line;
*endian = img->image->byte_order;
return (img->data);
}

40
mlx/mlx_hook.c Normal file
View file

@ -0,0 +1,40 @@
/*
** mlx_hook.c for MiniLibX in
**
** Made by Charlie Root
** Login <ol@epitech.net>
**
** Started on Thu Aug 3 11:49:06 2000 Charlie Root
** Last update Fri Jan 28 17:05:28 2005 Olivier Crouzet
*/
#include "mlx_int.h"
int mlx_hook(t_win_list *win, int x_event, int x_mask,
int (*funct)(),void *param)
{
win->hooks[x_event].hook = funct;
win->hooks[x_event].param = param;
win->hooks[x_event].mask = x_mask;
}
int mlx_do_key_autorepeatoff(t_xvar *xvar)
{
XAutoRepeatOff(xvar->display);
}
int mlx_do_key_autorepeaton(t_xvar *xvar)
{
XAutoRepeatOn(xvar->display);
}
int mlx_do_sync(t_xvar *xvar)
{
XSync(xvar->display, False);
}

99
mlx/mlx_init.c Normal file
View file

@ -0,0 +1,99 @@
/*
** mlx_init.c for MiniLibX in
**
** Made by Charlie Root
** Login <ol@epitech.net>
**
** Started on Mon Jul 31 16:52:42 2000 Charlie Root
** Last update Fri Jan 28 17:05:09 2005 Olivier Crouzet
*/
#include "mlx_int.h"
void *mlx_init()
{
t_xvar *xvar;
if (!(xvar = malloc(sizeof(*xvar))))
return ((void*)0);
if ((xvar->display = XOpenDisplay("")) == 0)
{
free(xvar);
return ((void*)0);
}
xvar->screen = DefaultScreen(xvar->display);
xvar->root = DefaultRootWindow(xvar->display);
xvar->cmap = DefaultColormap(xvar->display,xvar->screen);
xvar->depth = DefaultDepth(xvar->display,xvar->screen);
if (mlx_int_get_visual(xvar)==-1)
{
printf(ERR_NO_TRUECOLOR);
exit(1);
}
xvar->win_list = 0;
xvar->loop_hook = 0;
xvar->loop_param = (void *)0;
xvar->do_flush = 1;
xvar->wm_delete_window = XInternAtom (xvar->display, "WM_DELETE_WINDOW", False);
xvar->wm_protocols = XInternAtom (xvar->display, "WM_PROTOCOLS", False);
mlx_int_deal_shm(xvar);
if (xvar->private_cmap)
xvar->cmap = XCreateColormap(xvar->display,xvar->root,
xvar->visual,AllocNone);
mlx_int_rgb_conversion(xvar);
xvar->end_loop = 0;
return (xvar);
}
/*
** pshm_format of -1 : Not XYBitmap|XYPixmap|ZPixmap
** alpha libX need a check of the DISPLAY env var, or shm is allowed
** in remote Xserver connections.
*/
int mlx_int_deal_shm(t_xvar *xvar)
{
int use_pshm;
int bidon;
char *dpy;
char buff[33];
xvar->use_xshm = XShmQueryVersion(xvar->display,&bidon,&bidon,&(use_pshm));
if (xvar->use_xshm && use_pshm)
xvar->pshm_format = XShmPixmapFormat(xvar->display);
else
xvar->pshm_format = -1;
gethostname(buff,32);
dpy = getenv(ENV_DISPLAY);
if (dpy && strlen(dpy) && *dpy!=':' && strncmp(dpy,buff,strlen(buff)) &&
strncmp(dpy,LOCALHOST,strlen(LOCALHOST)) )
{
xvar->pshm_format = -1;
xvar->use_xshm = 0;
}
}
/*
** TrueColor Visual is needed to have *_mask correctly set
*/
int mlx_int_rgb_conversion(t_xvar *xvar)
{
bzero(xvar->decrgb,sizeof(int)*6);
while (!(xvar->visual->red_mask&1))
{ xvar->visual->red_mask >>= 1; xvar->decrgb[0] ++; }
while (xvar->visual->red_mask&1)
{ xvar->visual->red_mask >>= 1; xvar->decrgb[1] ++; }
while (!(xvar->visual->green_mask&1))
{ xvar->visual->green_mask >>= 1; xvar->decrgb[2] ++; }
while (xvar->visual->green_mask&1)
{ xvar->visual->green_mask >>= 1; xvar->decrgb[3] ++; }
while (!(xvar->visual->blue_mask&1))
{ xvar->visual->blue_mask >>= 1; xvar->decrgb[4] ++; }
while (xvar->visual->blue_mask&1)
{ xvar->visual->blue_mask >>= 1; xvar->decrgb[5] ++; }
}

140
mlx/mlx_int.h Normal file
View file

@ -0,0 +1,140 @@
/*
** mlx_int.h for mlx in
**
** Made by Charlie Root
** Login <ol@epitech.net>
**
** Started on Mon Jul 31 16:45:48 2000 Charlie Root
** Last update Wed May 25 16:44:16 2011 Olivier Crouzet
*/
/*
** Internal settings for MiniLibX
*/
#ifndef MLX_INT_H
# define MLX_INT_H
# include <stdlib.h>
# include <stdio.h>
# include <string.h>
# include <unistd.h>
# include <fcntl.h>
# include <sys/mman.h>
# include <X11/Xlib.h>
# include <X11/Xutil.h>
# include <sys/ipc.h>
# include <sys/shm.h>
# include <X11/extensions/XShm.h>
# include <X11/XKBlib.h>
/* #include <X11/xpm.h> */
# define MLX_TYPE_SHM_PIXMAP 3
# define MLX_TYPE_SHM 2
# define MLX_TYPE_XIMAGE 1
# define MLX_MAX_EVENT LASTEvent
# define ENV_DISPLAY "DISPLAY"
# define LOCALHOST "localhost"
# define ERR_NO_TRUECOLOR "MinilibX Error : No TrueColor Visual available.\n"
# define WARN_SHM_ATTACH "MinilibX Warning : X server can't attach shared memory.\n"
typedef struct s_xpm_col
{
int name;
int col;
} t_xpm_col;
struct s_col_name
{
char *name;
int color;
};
typedef struct s_event_list
{
int mask;
int (*hook)();
void *param;
} t_event_list;
typedef struct s_win_list
{
Window window;
GC gc;
struct s_win_list *next;
int (*mouse_hook)();
int (*key_hook)();
int (*expose_hook)();
void *mouse_param;
void *key_param;
void *expose_param;
t_event_list hooks[MLX_MAX_EVENT];
} t_win_list;
typedef struct s_img
{
XImage *image;
Pixmap pix;
GC gc;
int size_line;
int bpp;
int width;
int height;
int type;
int format;
char *data;
XShmSegmentInfo shm;
} t_img;
typedef struct s_xvar
{
Display *display;
Window root;
int screen;
int depth;
Visual *visual;
Colormap cmap;
int private_cmap;
t_win_list *win_list;
int (*loop_hook)();
void *loop_param;
int use_xshm;
int pshm_format;
int do_flush;
int decrgb[6];
Atom wm_delete_window;
Atom wm_protocols;
int end_loop;
} t_xvar;
int mlx_int_do_nothing();
int mlx_get_color_value();
int mlx_int_get_good_color();
int mlx_int_find_in_pcm();
int mlx_int_anti_resize_win();
int mlx_int_wait_first_expose();
int mlx_int_rgb_conversion();
int mlx_int_deal_shm();
void *mlx_int_new_xshm_image();
char **mlx_int_str_to_wordtab();
void *mlx_new_image();
int shm_att_pb();
int mlx_int_get_visual(t_xvar *xvar);
int mlx_int_set_win_event_mask(t_xvar *xvar);
int mlx_int_str_str_cote(char *str,char *find,int len);
int mlx_int_str_str(char *str,char *find,int len);
#endif

View file

@ -0,0 +1,28 @@
/*
** mlx_int_anti_resize_win.c for MiniLibX in
**
** Made by Charlie Root
** Login <ol@epitech.net>
**
** Started on Tue Aug 8 14:31:05 2000 Charlie Root
** Last update Tue Sep 25 15:56:58 2001 Charlie Root
*/
#include "mlx_int.h"
int mlx_int_anti_resize_win(t_xvar *xvar,Window win,int w,int h)
{
XSizeHints hints;
long toto;
XGetWMNormalHints(xvar->display,win,&hints,&toto);
hints.width = w;
hints.height = h;
hints.min_width = w;
hints.min_height = h;
hints.max_width = w;
hints.max_height = h;
hints.flags = PPosition | PSize | PMinSize | PMaxSize;
XSetWMNormalHints(xvar->display,win,&hints);
}

16
mlx/mlx_int_do_nothing.c Normal file
View file

@ -0,0 +1,16 @@
/*
** mlx_int_do_nothing.c for MiniLibX in
**
** Made by Charlie Root
** Login <ol@epitech.net>
**
** Started on Tue Aug 8 12:58:24 2000 Charlie Root
** Last update Tue Sep 25 15:56:22 2001 Charlie Root
*/
int mlx_int_do_nothing(void *param)
{
}

39
mlx/mlx_int_get_visual.c Normal file
View file

@ -0,0 +1,39 @@
/*
** mlx_int_get_visual.c for MinilibX in
**
** Made by Charlie Root
** Login <ol@epitech.net>
**
** Started on Wed Oct 3 17:01:51 2001 Charlie Root
** Last update Thu Oct 4 15:00:45 2001 Charlie Root
*/
#include "mlx_int.h"
/*
** We need a private colormap for non-default Visual.
*/
int mlx_int_get_visual(t_xvar *xvar)
{
XVisualInfo *vi;
XVisualInfo template;
int nb_item;
xvar->private_cmap = 0;
xvar->visual = DefaultVisual(xvar->display,xvar->screen);
if (xvar->visual->class == TrueColor)
return (0);
template.class = TrueColor;
template.depth = xvar->depth;
if (!(vi = XGetVisualInfo(xvar->display,VisualDepthMask|VisualClassMask,
&template,&nb_item)) )
return (-1);
xvar->visual = vi->visual;
xvar->private_cmap = 1;
return (0);
}

100
mlx/mlx_int_param_event.c Normal file
View file

@ -0,0 +1,100 @@
/*
** mlx_int_param_event.c for MinilibX in
**
** Made by Charlie Root
** Login <ol@epitech.net>
**
** Started on Mon Jul 31 16:37:50 2000 Charlie Root
** Last update Wed Oct 6 13:14:52 2004 Olivier Crouzet
*/
#include "mlx_int.h"
int mlx_int_param_undef()
{
}
int mlx_int_param_KeyPress(t_xvar *xvar, XEvent *ev, t_win_list *win)
{
win->hooks[KeyPress].hook(XkbKeycodeToKeysym(xvar->display,
ev->xkey.keycode, 0, 0),
win->hooks[KeyPress].param);
}
int mlx_int_param_KeyRelease(t_xvar *xvar, XEvent *ev, t_win_list *win)
{
win->hooks[KeyRelease].hook(XkbKeycodeToKeysym(xvar->display,
ev->xkey.keycode, 0, 0),
win->hooks[KeyRelease].param);
}
int mlx_int_param_ButtonPress(t_xvar *xvar, XEvent *ev, t_win_list *win)
{
win->hooks[ButtonPress].hook(ev->xbutton.button,ev->xbutton.x,ev->xbutton.y,
win->hooks[ButtonPress].param);
}
int mlx_int_param_ButtonRelease(t_xvar *xvar, XEvent *ev, t_win_list *win)
{
win->hooks[ButtonRelease].hook(ev->xbutton.button,
ev->xbutton.x, ev->xbutton.y,
win->hooks[ButtonRelease].param);
}
int mlx_int_param_MotionNotify(t_xvar *xvar, XEvent *ev, t_win_list *win)
{
win->hooks[MotionNotify].hook(ev->xbutton.x,ev->xbutton.y,
win->hooks[MotionNotify].param);
}
int mlx_int_param_Expose(t_xvar *xvar, XEvent *ev, t_win_list *win)
{
if (!ev->xexpose.count)
win->hooks[Expose].hook(win->hooks[Expose].param);
}
int mlx_int_param_generic(t_xvar *xvar, XEvent *ev, t_win_list *win)
{
win->hooks[ev->type].hook(win->hooks[ev->type].param);
}
int (*(mlx_int_param_event[]))() =
{
mlx_int_param_undef, /* 0 */
mlx_int_param_undef,
mlx_int_param_KeyPress,
mlx_int_param_KeyRelease, /* 3 */
mlx_int_param_ButtonPress,
mlx_int_param_ButtonRelease,
mlx_int_param_MotionNotify, /* 6 */
mlx_int_param_generic,
mlx_int_param_generic,
mlx_int_param_generic,
mlx_int_param_generic,
mlx_int_param_generic,
mlx_int_param_Expose, /* 12 */
mlx_int_param_generic,
mlx_int_param_generic,
mlx_int_param_generic,
mlx_int_param_generic,
mlx_int_param_generic,
mlx_int_param_generic,
mlx_int_param_generic,
mlx_int_param_generic,
mlx_int_param_generic,
mlx_int_param_generic,
mlx_int_param_generic,
mlx_int_param_generic,
mlx_int_param_generic,
mlx_int_param_generic,
mlx_int_param_generic,
mlx_int_param_generic,
mlx_int_param_generic,
mlx_int_param_generic,
mlx_int_param_generic,
mlx_int_param_generic,
mlx_int_param_generic,
mlx_int_param_generic,
mlx_int_param_generic
};

View file

@ -0,0 +1,34 @@
/*
** mlx_int_set_win_event_mask.c for MiniLibX in
**
** Made by Charlie Root
** Login <ol@epitech.net>
**
** Started on Thu Aug 3 11:49:06 2000 Charlie Root
** Last update Fri Feb 23 17:07:42 2001 Charlie Root
*/
#include "mlx_int.h"
int mlx_int_set_win_event_mask(t_xvar *xvar)
{
t_win_list *win;
int mask;
int i;
XSetWindowAttributes xwa;
win = xvar->win_list;
while (win)
{
xwa.event_mask = 0;
i = MLX_MAX_EVENT;
while (i--)
xwa.event_mask |= win->hooks[i].mask;
XChangeWindowAttributes(xvar->display, win->window, CWEventMask, &xwa);
win = win->next;
}
}

View file

@ -0,0 +1,113 @@
/*
** mlx_int_str_to_wordtab.c for MinilibX in
**
** Made by Charlie Root
** Login <ol@epitech.net>
**
** Started on Wed Sep 13 11:36:09 2000 Charlie Root
** Last update Fri Dec 14 11:02:09 2001 Charlie Root
*/
#include "mlx_int.h"
int mlx_int_str_str(char *str,char *find,int len)
{
int len_f;
int pos;
char *s;
char *f;
len_f = strlen(find);
if (len_f>len)
return (-1);
pos = 0;
while (*(str+len_f-1))
{
s = str;
f = find;
while (*(f++) == *(s++))
if (!*f)
return (pos);
str ++;
pos ++;
}
return (-1);
}
int mlx_int_str_str_cote(char *str,char *find,int len)
{
int len_f;
int pos;
char *s;
char *f;
int cote;
len_f = strlen(find);
if (len_f>len)
return (-1);
cote = 0;
pos = 0;
while (*(str+len_f-1))
{
if (*str=='"')
cote = 1-cote;
if (!cote)
{
s = str;
f = find;
while (*(f++) == *(s++))
if (!*f)
return (pos);
}
str ++;
pos ++;
}
return (-1);
}
char **mlx_int_str_to_wordtab(char *str)
{
char **tab;
int pos;
int nb_word;
int len;
len = strlen(str);
nb_word = 0;
pos = 0;
while (pos<len)
{
while (*(str+pos)==' ' || *(str+pos)=='\t')
pos ++;
if (*(str+pos))
nb_word ++;
while (*(str+pos) && *(str+pos)!=' ' && *(str+pos)!='\t')
pos ++;
}
if (!(tab = malloc((1+nb_word)*sizeof(*tab))))
return ((char **)0);
nb_word = 0;
pos = 0;
while (pos<len)
{
while (*(str+pos)==' ' || *(str+pos)=='\t')
{
*(str+pos) = 0;
pos ++;
}
if (*(str+pos))
{
tab[nb_word] = str+pos;
nb_word ++;
}
while (*(str+pos) && *(str+pos)!=' ' && *(str+pos)!='\t')
pos ++;
}
tab[nb_word] = 0;
return (tab);
}

View file

@ -0,0 +1,23 @@
/*
** mlx_int_wait_first_expose.c for minilibx in
**
** Made by olivier crouzet
** Login <ol@epita.fr>
**
** Started on Tue Oct 17 09:26:45 2000 olivier crouzet
** Last update Fri Feb 23 17:27:10 2001 Charlie Root
*/
#include "mlx_int.h"
int mlx_int_wait_first_expose(t_xvar *xvar,Window win)
{
XEvent ev;
XWindowEvent(xvar->display,win,ExposureMask,&ev);
XPutBackEvent(xvar->display,&ev);
}

22
mlx/mlx_key_hook.c Normal file
View file

@ -0,0 +1,22 @@
/*
** mlx_key_hook.c for MiniLibX in
**
** Made by Charlie Root
** Login <ol@epitech.net>
**
** Started on Thu Aug 3 11:49:06 2000 Charlie Root
** Last update Fri Feb 23 17:10:09 2001 Charlie Root
*/
#include "mlx_int.h"
int mlx_key_hook(t_win_list *win,int (*funct)(),void *param)
{
win->hooks[KeyRelease].hook = funct;
win->hooks[KeyRelease].param = param;
win->hooks[KeyRelease].mask = KeyReleaseMask;
}

96
mlx/mlx_lib_xpm.c Normal file
View file

@ -0,0 +1,96 @@
/*
** mlx_xpm.c for minilibX in
**
** Made by Charlie Root
** Login <ol@epitech.net>
**
** Started on Fri Dec 8 11:07:24 2000 Charlie Root
** Last update Thu Oct 4 16:00:22 2001 Charlie Root
*/
#include "mlx_int.h"
void *mlx_int_xpm_f_image(t_xvar *xvar,int *width,int *height,
int (*xpm_func)(),void *param)
{
XImage *img1;
XImage *img2;
t_img *im2;
XpmAttributes xpm_att;
xpm_att.visual = xvar->visual;
xpm_att.colormap = xvar->cmap;
xpm_att.depth = xvar->depth;
xpm_att.bitmap_format = ZPixmap;
xpm_att.valuemask = XpmDepth|XpmBitmapFormat|XpmVisual|XpmColormap;
if (xpm_func(xvar->display,param,&img1,&img2,&xpm_att))
return ((void *)0);
if (img2)
XDestroyImage(img2);
if (!(im2 = (void *)mlx_new_image(xvar,img1->width,img1->height)))
{
XDestroyImage(img1);
return ((void *)0);
}
*width = img1->width;
*height = img1->height;
if (mlx_int_egal_img(im2->image,img1))
{
bcopy(img1->data,im2->data,img1->height*img1->bytes_per_line);
XDestroyImage(img1);
return (im2);
}
if (im2->type==MLX_TYPE_SHM_PIXMAP)
{
XFreePixmap(xvar->display,im2->pix);
im2->pix = XCreatePixmap(xvar->display,xvar->root,
*width,*height,xvar->depth);
}
if (im2->type>MLX_TYPE_XIMAGE)
{
XShmDetach(xvar->display,&(im2->shm));
shmdt(im2->data);
}
XDestroyImage(im2->image);
im2->image = img1;
im2->data = img1->data;
im2->type = MLX_TYPE_XIMAGE;
im2->size_line = img1->bytes_per_line;
im2->bpp = img1->bits_per_pixel;
return (im2);
}
int mlx_int_egal_img(XImage *img1,XImage *img2)
{
if (img1->width!=img2->width || img1->height!=img2->height ||
img1->xoffset!=img2->xoffset || img1->format!=img2->format ||
img1->byte_order!=img2->byte_order ||
img1->bitmap_unit!=img2->bitmap_unit ||
img1->bitmap_bit_order!=img2->bitmap_bit_order ||
img1->bitmap_pad!=img2->bitmap_pad || img1->depth!=img2->depth ||
img1->bytes_per_line!=img2->bytes_per_line ||
img1->bits_per_pixel!=img2->bits_per_pixel ||
img1->red_mask!=img2->red_mask || img1->green_mask!=img2->green_mask ||
img1->blue_mask!=img2->blue_mask )
return (0);
return (1);
}
void *mlx_xpm_file_to_image(t_xvar *xvar,char *filename,
int *width,int *height)
{
return (mlx_int_xpm_f_image(xvar,width,height,XpmReadFileToImage,filename));
}
void *mlx_xpm_to_image(t_xvar *xvar,char **data,int *width,int *height)
{
return (mlx_int_xpm_f_image(xvar,width,height,XpmCreateImageFromData,(void *)data));
}

Some files were not shown because too many files have changed in this diff Show more