├── .gitignore ├── LICENSE ├── Makefile ├── README.md ├── assets ├── ._main.gif └── main.gif ├── author ├── ft_select.en.pdf ├── inc └── ft_select.h ├── libft ├── .DS_Store ├── .gitignore ├── Makefile ├── ft_atoi.c ├── ft_bzero.c ├── ft_capitalize.c ├── ft_copyuntil.c ├── ft_count2darray.c ├── ft_countwords.c ├── ft_countwordsall.c ├── ft_freestrarr.c ├── ft_get_parent_path.c ├── ft_intlen.c ├── ft_isalnum.c ├── ft_isalpha.c ├── ft_isascii.c ├── ft_isdigit.c ├── ft_isemptystr.c ├── ft_islower.c ├── ft_isprint.c ├── ft_isupper.c ├── ft_itoa.c ├── ft_lst_reverse.c ├── ft_lstadd.c ├── ft_lstaddback.c ├── ft_lstdel.c ├── ft_lstdelone.c ├── ft_lstiter.c ├── ft_lstmap.c ├── ft_lstnew.c ├── ft_memalloc.c ├── ft_memccpy.c ├── ft_memchr.c ├── ft_memcmp.c ├── ft_memcpy.c ├── ft_memdel.c ├── ft_memmove.c ├── ft_memset.c ├── ft_pathjoin.c ├── ft_putchar.c ├── ft_putchar_fd.c ├── ft_putendl.c ├── ft_putendl_fd.c ├── ft_putnbr.c ├── ft_putnbr_fd.c ├── ft_putnstr.c ├── ft_putstr.c ├── ft_putstr_fd.c ├── ft_realloc.c ├── ft_strarrmax.c ├── ft_strcat.c ├── ft_strchr.c ├── ft_strclr.c ├── ft_strcmp.c ├── ft_strcpy.c ├── ft_strdel.c ├── ft_strdup.c ├── ft_strendswith.c ├── ft_strequ.c ├── ft_striter.c ├── ft_striteri.c ├── ft_strjoin.c ├── ft_strjoinch.c ├── ft_strjoinchcl.c ├── ft_strjoincl.c ├── ft_strlcat.c ├── ft_strlen.c ├── ft_strmap.c ├── ft_strmapi.c ├── ft_strncat.c ├── ft_strnchr.c ├── ft_strncmp.c ├── ft_strncpy.c ├── ft_strndup.c ├── ft_strnequ.c ├── ft_strnew.c ├── ft_strnstr.c ├── ft_strpop.c ├── ft_strrchr.c ├── ft_strreplace.c ├── ft_strsplit.c ├── ft_strsplitall.c ├── ft_strstartswith.c ├── ft_strstr.c ├── ft_strsub.c ├── ft_strtrim.c ├── ft_tolower.c ├── ft_toupper.c ├── get_next_line.c └── includes │ └── libft.h ├── norme.en.pdf └── src ├── actions.c ├── column_display.c ├── init_args.c ├── main.c ├── print_usage.c ├── signal_handler.c ├── utils.c ├── utils2.c └── utils3.c /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Object files 5 | *.o 6 | *.ko 7 | *.obj 8 | *.elf 9 | 10 | # Linker output 11 | *.ilk 12 | *.map 13 | *.exp 14 | 15 | # Precompiled Headers 16 | *.gch 17 | *.pch 18 | 19 | # Libraries 20 | *.lib 21 | *.a 22 | *.la 23 | *.lo 24 | 25 | # Shared objects (inc. Windows DLLs) 26 | *.dll 27 | *.so 28 | *.so.* 29 | *.dylib 30 | 31 | # Executables 32 | *.exe 33 | *.out 34 | *.app 35 | *.i*86 36 | *.x86_64 37 | *.hex 38 | 39 | # Debug files 40 | *.dSYM/ 41 | *.su 42 | *.idb 43 | *.pdb 44 | 45 | # Kernel Module Compile Results 46 | *.mod* 47 | *.cmd 48 | .tmp_versions/ 49 | modules.order 50 | Module.symvers 51 | Mkfile.old 52 | dkms.conf 53 | 54 | ft_select 55 | test 56 | .nfs.* 57 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Nick Rameau 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # **************************************************************************** # 2 | # # 3 | # ::: :::::::: # 4 | # Makefile :+: :+: :+: # 5 | # +:+ +:+ +:+ # 6 | # By: jrameau +#+ +:+ +#+ # 7 | # +#+#+#+#+#+ +#+ # 8 | # Created: 2016/12/13 11:43:23 by jrameau #+# #+# # 9 | # Updated: 2017/07/04 07:12:16 by nick ### ########.fr # 10 | # # 11 | # **************************************************************************** # 12 | 13 | # Project file 14 | NAME = ft_select 15 | 16 | # Project builds and dirs 17 | SRCDIR = ./src/ 18 | SRCNAMES = $(shell ls $(SRCDIR) | grep -E ".+\.c") 19 | SRC = $(addprefix $(SRCDIR), $(SRCNAMES)) 20 | INC = ./inc/ 21 | BUILDDIR = ./build/ 22 | BUILDOBJS = $(addprefix $(BUILDDIR), $(SRCNAMES:.c=.o)) 23 | 24 | # Libft builds and dirs 25 | LIBDIR = ./libft/ 26 | LIBFT = ./libft/libft.a 27 | LIBINC = ./libft/includes/ 28 | 29 | # Optimization and Compiler flags and commands 30 | CC = gcc 31 | CFLAGS = -Wall -Werror -Wextra -D_XOPEN_SOURCE=500 32 | 33 | # Debugging flags 34 | DEBUG = -ggdb3 35 | 36 | # Main rule 37 | all: $(BUILDDIR) $(LIBFT) $(NAME) 38 | 39 | # Object dir rule 40 | $(BUILDDIR): 41 | mkdir -p $(BUILDDIR) 42 | 43 | # Objects rule 44 | $(BUILDDIR)%.o:$(SRCDIR)%.c 45 | $(CC) $(CFLAGS) -I$(LIBINC) -I$(INC) -o $@ -c $< 46 | 47 | # Project file rule 48 | $(NAME): $(BUILDOBJS) 49 | $(CC) $(CFLAGS) -o $(NAME) $(BUILDOBJS) $(LIBFT) -ltermcap 50 | 51 | # Libft rule 52 | $(LIBFT): 53 | make -C $(LIBDIR) 54 | 55 | # Cleaning up the build files 56 | clean: 57 | rm -rf $(BUILDDIR) 58 | make -C $(LIBDIR) clean 59 | 60 | # Getting rid of the project file 61 | fclean: clean 62 | rm -rf $(NAME) 63 | make -C $(LIBDIR) fclean 64 | 65 | # Do both of the above 66 | re: fclean all 67 | 68 | # Just in case those files exist in the root dir 69 | .PHONY: all fclean clean re 70 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # FT_SELECT - @42Born2Code 2 | 3 | A robust file browser and manager in the terminal. 4 | 5 | ![](https://raw.githubusercontent.com/R4meau/ft_select/master/assets/main.gif) 6 | 7 | ## About 8 | 9 | [ft_select](https://github.com/R4meau/ft_select/blob/master/ft_select.en.pdf) is the third project of the Unix branch at 42. The goal for this project is to learn more about screen oriented programs using termcap/terminfo libraries in the C programming language. It's originally just an argument viewer in the terminal but I decided to take it one step further and gave it browsing capabilities on the file system. 10 | 11 | The main goal, however, is to build my own functional shell. Something like `zsh` and `bash`, this is what the next project at my school is about and ft_select is just a small part of it. 12 | 13 | ## How to use it 14 | 15 | ### Requirements 16 | 17 | To be able to build and run this program, you'll need: 18 | 19 | * A Unix system 20 | * GNU make (v3.81) 21 | * GCC (v4.2.1) 22 | 23 | Those are the versions used during development. 24 | 25 | If you're on Linux, make sure you install 26 | 27 | * The termcap library: 28 | 29 | sudo apt-get install libncurses5-dev 30 | 31 | ### Building 32 | 33 | 1. Download/Clone this repo 34 | 35 | git clone https://github.com/r4meau/ft_select 36 | 37 | 2. `cd` into the root directory and run `make` 38 | 39 | cd ft_select 40 | make 41 | 42 | ### Running 43 | 44 | A basic usage would involve at least one argument: 45 | 46 | ./ft_select hello 47 | 48 | ## Features 49 | 50 | * If you pass a list of arguments to the program it gets displayed in your terminal. 51 | * You can move through the list using arrows. 52 | * One or more choices can be selected or un-selected with the `space` key. With 53 | each selection, the cursor will automatically position itself on the next element. 54 | * You can validate the selection with the `return` key, the list of choices will 55 | be sent back to the shell. This allows other programs to use the output of the program as their input. i.e: rm `./ft_select file1 file2 file3` 56 | * Re-organizes the display on window resize or displays a blank screen if the list cannot fit the in window. 57 | * Exits on `ESC` key 58 | * Pressing the `delete` or `backspace` keys removes an element from the list. In Real mode (Read below), it will automatically delete the active element from the system if it's a valid file/folder. So watch out! 59 | * Press `*` to select all and `-` to unselect all. 60 | * Colored output based on some supported file extensions. 61 | * Pressing the keys `O` and `B` opens a valid folder and goes back to the parent root directory respectively. 62 | * Handles interruption signals gracefully (ctrl + z, ctrl + c, kill, etc...) 63 | * Restores the screen to what it was before clearing it. 64 | * No memory leaks 65 | * Whole codebase is commented for easier browsing. 66 | 67 | ### Real mode 68 | 69 | You can launch the program in real mode. In real mode, when you press `delete`/`backspace` on a valid file or folder, it will be deleted on the system too. So again, watch out! I'm not responsible for any loss of your files. 70 | 71 | 72 | To run ft_select in real mode, use the flag `-r` or `--real` 73 | 74 | ./ft_select -r I love 42 75 | 76 | ### TODO 77 | 78 | * Add breadcrumb 79 | * Press `H` to see hidden/dot files 80 | * Add copy and move capabilities 81 | * Even better error handling 82 | 83 | ## NOTES 84 | 85 | * Don't mind the uneven tab widths, just set your editor's tab size to 4 (hard tabs, not soft) and it should be fine 86 | * You might not like the fact that I'm not adding curly braces for some if statements, curly braces add extra lines, and my school has a coding standard (The Norme) that you must follow to succeed the projects, this standard imposes some rules like 25 lines function limits etc... Read more about it [here](https://github.com/R4meau/ft_select/blob/master/norme.en.pdf). 87 | * I added a second branch called `original` which is the original code of the program before I started norming the code. It will lack some extra features that I didn't add back then. It's here to show me how I code initially and will help me see how much I improve over time. 88 | 89 | ### Resources 90 | 91 | Here's a list of resources (in no particular order) I found useful to complete this project: 92 | 93 | * [WikiBooks](https://en.wikibooks.org/wiki/Serial_Programming/termios) 94 | * [Daemon Systems](https://www.daemon-systems.org/man/terminfo.5.html) 95 | * [Cygwin](https://cygwin.com/ml/cygwin/2004-04/msg01158.html) 96 | 97 | ## Acknowledgment 98 | 99 | Thanks to my peers at 42, Christophe Gerbaudo (@cgerbaud) and Giacomo Guiulfo (@gguiulfo) who helped me go faster on this project by giving me some great tips. 100 | 101 | # Sponsors 102 | 103 | Sponsor 104 | 105 | Enjoy! 106 | -------------------------------------------------------------------------------- /assets/._main.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickdotht/ft_select/054e83def5eed7b379ace22c250d4744b9450a4a/assets/._main.gif -------------------------------------------------------------------------------- /assets/main.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickdotht/ft_select/054e83def5eed7b379ace22c250d4744b9450a4a/assets/main.gif -------------------------------------------------------------------------------- /author: -------------------------------------------------------------------------------- 1 | jrameau 2 | -------------------------------------------------------------------------------- /ft_select.en.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickdotht/ft_select/054e83def5eed7b379ace22c250d4744b9450a4a/ft_select.en.pdf -------------------------------------------------------------------------------- /inc/ft_select.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_select.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/06/27 20:50:04 by jrameau #+# #+# */ 9 | /* Updated: 2017/07/04 07:19:09 by nick ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef FT_SELECT_H 14 | # define FT_SELECT_H 15 | 16 | # include 17 | # include 18 | # include 19 | # include 20 | # include 21 | # include 22 | # include 23 | # include 24 | # include 25 | # include 26 | # include 27 | # include 28 | 29 | /* 30 | ** Predefined color/attribute codes 31 | */ 32 | 33 | # define C_COLOR "\033[35m" 34 | # define O_COLOR "\033[36m" 35 | # define H_COLOR "\033[34m" 36 | # define MAKEFILE_COLOR "\033[33m" 37 | # define DOT_COLOR "\033[32m" 38 | # define DEFAULT_COLOR "\033[0m" 39 | # define A_COLOR "\033[31m" 40 | # define REVERSE_VIDEO_COLOR "\033[7m" 41 | # define UNDERLINED "\033[4m" 42 | 43 | /* 44 | ** Supported keys during program execution 45 | */ 46 | 47 | # define ENTER_KEY 10 48 | # define ESC_KEY 27 49 | # define SPC_KEY 32 50 | # define STAR_KEY 42 51 | # define MINUS_KEY 45 52 | # define O_KEY 111 53 | # define B_KEY 98 54 | # define BSP_KEY 127 55 | # define LEFT_KEY 4479771 56 | # define UP_KEY 4283163 57 | # define RIGHT_KEY 4414235 58 | # define DOWN_KEY 4348699 59 | # define DEL_KEY 2117294875L 60 | 61 | /* 62 | ** File types supported by the program, will add more 63 | ** 64 | ** C_T - .c 65 | ** O_T - .o 66 | ** H_T - .h 67 | ** A_T - .a 68 | ** MAKEFILE_T - Makefile 69 | ** DOT_T - dot files (.gitignore, .vimrc) 70 | ** UNKNOWN_T - unknown or unsupported type 71 | */ 72 | 73 | typedef enum e_type 74 | { 75 | C_T, 76 | O_T, 77 | H_T, 78 | A_T, 79 | MAKEFILE_T, 80 | DOT_T, 81 | UNKNOWN_T 82 | } t_type; 83 | 84 | /* 85 | ** Cursor directions in the arguments/files browser 86 | ** 87 | ** DEFAULT_DIR is for unknown/unsupported keys 88 | */ 89 | 90 | typedef enum e_dir 91 | { 92 | UP_DIR, 93 | RIGHT_DIR, 94 | DOWN_DIR, 95 | LEFT_DIR, 96 | DEFAULT_DIR 97 | } t_dir; 98 | 99 | /* 100 | ** The list of arguments to display and edit 101 | ** 102 | ** value - The argument value 103 | ** is_selected - A boolean to know if the current argument is selected or not 104 | ** type - The type of the current argument 105 | ** prev - The previous argument 106 | ** next - The next argument 107 | */ 108 | 109 | typedef struct s_arg 110 | { 111 | char *value; 112 | int is_selected; 113 | t_type type; 114 | struct s_arg *prev; 115 | struct s_arg *next; 116 | } t_arg; 117 | 118 | /* 119 | ** A struct that holds any information we might need about the arguments list 120 | ** 121 | ** active_arg - The currently active argument 122 | ** old_attr - The old attributes before we apply our own 123 | ** attr - Our custom attributes 124 | ** argc - The arguments count 125 | ** term_name - The terminal name from the environment 126 | ** real_mode - A boolean to know if the program should really delete 127 | ** files from the system or not 128 | ** args - The arguments list 129 | ** selected_count - The amount of selected arguments for use when printing 130 | */ 131 | 132 | typedef struct s_select 133 | { 134 | t_arg **active_arg; 135 | struct termios old_attr; 136 | struct termios attr; 137 | int argc; 138 | char *term_name; 139 | int real_mode; 140 | t_arg *args; 141 | int selected_count; 142 | } t_select; 143 | 144 | /* 145 | ** The global variable that will be used during the whole execution 146 | ** 147 | ** I'm using a global variable so I can be able to free the memory once 148 | ** the program gets killed, there are no other methods of doing this. Unless we 149 | ** don't use signals anymore, but for this project, I wasn't allowed to use 150 | ** anything else. 151 | */ 152 | 153 | t_select g_select; 154 | 155 | /* 156 | ** src/actions.c 157 | */ 158 | 159 | void move(t_dir direction); 160 | 161 | /* 162 | ** src/column_display.c 163 | */ 164 | 165 | void column_display(); 166 | int count_columns(); 167 | void print_value_fd(t_arg *arg, int fd); 168 | 169 | /* 170 | ** src/init_args.c 171 | */ 172 | 173 | void init_args(char **av); 174 | void free_args(void); 175 | void delete_active_arg(void); 176 | void insert_arg(char *value); 177 | 178 | /* 179 | ** src/main.c 180 | */ 181 | 182 | void init_custom_conf(); 183 | void init_signal_handlers(); 184 | 185 | /* 186 | ** src/print_usage.c 187 | */ 188 | 189 | void print_usage(void); 190 | 191 | /* 192 | ** src/signal_handler.c 193 | */ 194 | 195 | void signal_handler(int signo); 196 | void stop_signal_handler(void); 197 | 198 | /* 199 | ** src/utils.c 200 | */ 201 | 202 | void validate_flag(char *arg); 203 | void on_key_press(void); 204 | 205 | /* 206 | ** src/utils2.c 207 | */ 208 | 209 | int count_max_arg_len(void); 210 | int ft_printnbr(int nbr); 211 | void reset_default_conf(void); 212 | void toggle_all_args(long key); 213 | 214 | /* 215 | ** src/utils3.c 216 | */ 217 | 218 | void system_delete_arg(char *path); 219 | 220 | #endif 221 | -------------------------------------------------------------------------------- /libft/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickdotht/ft_select/054e83def5eed7b379ace22c250d4744b9450a4a/libft/.DS_Store -------------------------------------------------------------------------------- /libft/.gitignore: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickdotht/ft_select/054e83def5eed7b379ace22c250d4744b9450a4a/libft/.gitignore -------------------------------------------------------------------------------- /libft/Makefile: -------------------------------------------------------------------------------- 1 | # **************************************************************************** # 2 | # # 3 | # ::: :::::::: # 4 | # Makefile :+: :+: :+: # 5 | # +:+ +:+ +:+ # 6 | # By: jrameau +#+ +:+ +#+ # 7 | # +#+#+#+#+#+ +#+ # 8 | # Created: 2016/09/21 14:58:27 by jrameau #+# #+# # 9 | # Updated: 2017/05/21 14:58:14 by jrameau ### ########.fr # 10 | # # 11 | # **************************************************************************** # 12 | 13 | NAME = libft.a 14 | CFLAGS = -Wall -Werror -Wextra -Iincludes -c 15 | FILES = $(shell ls | grep -E ".+\.c") 16 | OBJ = $(FILES:%.c=%.o) 17 | 18 | all: $(NAME) 19 | 20 | # This won't run if the .o files don't exist or are not modified 21 | $(NAME): $(OBJ) 22 | @ar rcs $(NAME) $(OBJ) 23 | 24 | # This won't run if the source files don't exist or are not modified 25 | $(OBJ): $(FILES) 26 | @gcc $(CFLAGS) $(FILES) 27 | 28 | clean: 29 | @rm -f $(OBJ) 30 | 31 | fclean: clean 32 | @rm -f $(NAME) 33 | 34 | re: fclean all 35 | 36 | # I use .PHONY to make sure that gnu make will still run even if files called 37 | # clean / fclean / all and re already exist in the directory 38 | .PHONY: clean fclean all re 39 | -------------------------------------------------------------------------------- /libft/ft_atoi.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_atoi.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/23 04:34:26 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/23 04:34:27 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_atoi(const char *str) 16 | { 17 | int i; 18 | int num; 19 | int sign; 20 | 21 | i = 0; 22 | num = 0; 23 | sign = 1; 24 | while (*(str + i) == '\n' || 25 | *(str + i) == '\t' || 26 | *(str + i) == '\r' || 27 | *(str + i) == '\v' || 28 | *(str + i) == '\f' || 29 | *(str + i) == ' ') 30 | i++; 31 | if (*(str + i) == '-') 32 | sign = -1; 33 | if (*(str + i) == '-' || *(str + i) == '+') 34 | i++; 35 | while (*(str + i) && *(str + i) >= '0' && *(str + i) <= '9') 36 | num = num * 10 + (*(str + i++) - '0'); 37 | return (num * sign); 38 | } 39 | -------------------------------------------------------------------------------- /libft/ft_bzero.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_bzero.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/21 15:52:51 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/21 15:52:53 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_bzero(void *s, size_t n) 16 | { 17 | char *ptr; 18 | size_t i; 19 | 20 | if (!n) 21 | return ; 22 | ptr = s; 23 | i = 0; 24 | while (i < n) 25 | *(ptr + i++) = 0; 26 | } 27 | -------------------------------------------------------------------------------- /libft/ft_capitalize.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_capitalize.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/29 15:51:24 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/29 15:51:26 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_capitalize(char *s) 16 | { 17 | char *new; 18 | int i; 19 | 20 | if (!s) 21 | return (NULL); 22 | new = ft_strnew(ft_strlen(s)); 23 | new[0] = ft_toupper(s[0]); 24 | i = 0; 25 | while (*(s + ++i)) 26 | if (!ft_isalnum(s[i - 1]) && ft_isalnum(s[i])) 27 | new[i] = ft_toupper(s[i]); 28 | else 29 | new[i] = s[i]; 30 | return (new); 31 | } 32 | -------------------------------------------------------------------------------- /libft/ft_copyuntil.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* copyuntil.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/11/26 05:49:22 by jrameau #+# #+# */ 9 | /* Updated: 2016/11/26 05:52:26 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_copyuntil(char **dst, char *src, char c) 16 | { 17 | int i; 18 | int count; 19 | int pos; 20 | 21 | i = -1; 22 | count = 0; 23 | while (src[++i]) 24 | if (src[i] == c) 25 | break ; 26 | pos = i; 27 | if (!(*dst = ft_strnew(i))) 28 | return (0); 29 | while (src[count] && count < i) 30 | { 31 | if (!(*dst = ft_strjoinch(*dst, src[count]))) 32 | return (0); 33 | count++; 34 | } 35 | return (pos); 36 | } 37 | -------------------------------------------------------------------------------- /libft/ft_count2darray.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_count2darray.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/06/27 21:52:38 by jrameau #+# #+# */ 9 | /* Updated: 2017/07/02 17:56:03 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include 14 | 15 | int ft_count2darray(char **arr) 16 | { 17 | int i; 18 | int count; 19 | 20 | i = -1; 21 | count = 0; 22 | while (arr[++i]) 23 | count++; 24 | return (count); 25 | } 26 | -------------------------------------------------------------------------------- /libft/ft_countwords.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_countwords.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/28 15:17:40 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/28 15:17:41 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_countwords(char const *str, char c) 16 | { 17 | int count; 18 | int i; 19 | 20 | i = 0; 21 | count = 0; 22 | while (str[i]) 23 | { 24 | while (str[i] == c) 25 | i++; 26 | if (str[i] != c && str[i] != '\0') 27 | count++; 28 | while (str[i] != c && str[i] != '\0') 29 | i++; 30 | } 31 | return (count); 32 | } 33 | -------------------------------------------------------------------------------- /libft/ft_countwordsall.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_countwordsall.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/28 15:17:40 by jrameau #+# #+# */ 9 | /* Updated: 2017/05/11 00:23:19 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_countwordsall(char const *str) 16 | { 17 | int count; 18 | int i; 19 | 20 | i = 0; 21 | count = 0; 22 | while (str[i]) 23 | { 24 | while (IS_SPACE(str[i])) 25 | i++; 26 | if (!IS_SPACE(str[i]) && str[i] != '\0') 27 | count++; 28 | while (!IS_SPACE(str[i]) && str[i] != '\0') 29 | i++; 30 | } 31 | return (count); 32 | } 33 | -------------------------------------------------------------------------------- /libft/ft_freestrarr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_freestrarr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/05/18 16:00:12 by jrameau #+# #+# */ 9 | /* Updated: 2017/05/18 16:36:33 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_freestrarr(char **arr) 16 | { 17 | int i; 18 | 19 | if (!arr) 20 | return ; 21 | i = -1; 22 | while (arr[++i]) 23 | { 24 | free(arr[i]); 25 | } 26 | free(arr); 27 | arr = NULL; 28 | } 29 | -------------------------------------------------------------------------------- /libft/ft_get_parent_path.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_get_parent_path.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/07/01 16:43:14 by jrameau #+# #+# */ 9 | /* Updated: 2017/07/02 17:56:28 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include 14 | 15 | char *ft_get_parent_path(char *path) 16 | { 17 | char *last_slash; 18 | char *parent; 19 | 20 | last_slash = strrchr(path, '/'); 21 | parent = strndup(path, last_slash - path); 22 | return (parent); 23 | } 24 | -------------------------------------------------------------------------------- /libft/ft_intlen.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_intlen.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/12/22 13:09:38 by jrameau #+# #+# */ 9 | /* Updated: 2017/04/19 22:47:25 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | int higher_nums(int num) 14 | { 15 | if (num >= 10000000) 16 | { 17 | if (num >= 1000000000) 18 | return (10); 19 | if (num >= 100000000) 20 | return (9); 21 | return (8); 22 | } 23 | if (num >= 1000000) 24 | return (7); 25 | return (6); 26 | } 27 | 28 | int ft_intlen(int num) 29 | { 30 | if (num >= 100000) 31 | return (higher_nums(num)); 32 | else 33 | { 34 | if (num >= 1000) 35 | { 36 | if (num >= 10000) 37 | return (5); 38 | return (4); 39 | } 40 | else 41 | { 42 | if (num >= 100) 43 | return (3); 44 | if (num >= 10) 45 | return (2); 46 | } 47 | return (1); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /libft/ft_isalnum.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isalnum.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/23 20:54:25 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/23 20:54:26 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_isalnum(int c) 16 | { 17 | return (ft_isalpha(c) || ft_isdigit(c)); 18 | } 19 | -------------------------------------------------------------------------------- /libft/ft_isalpha.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isalpha.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/23 12:39:45 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/23 12:39:46 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_isalpha(int c) 16 | { 17 | return (ft_islower(c) || ft_isupper(c)); 18 | } 19 | -------------------------------------------------------------------------------- /libft/ft_isascii.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isascii.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/23 21:10:58 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/24 03:25:02 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_isascii(int c) 16 | { 17 | return (c >= 0 && c <= 127); 18 | } 19 | -------------------------------------------------------------------------------- /libft/ft_isdigit.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isdigit.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/23 17:00:15 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/23 23:43:14 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_isdigit(int c) 16 | { 17 | return (c <= '9' && c >= '0'); 18 | } 19 | -------------------------------------------------------------------------------- /libft/ft_isemptystr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isemptystr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/05/11 00:03:25 by jrameau #+# #+# */ 9 | /* Updated: 2017/05/21 01:09:11 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_isemptystr(char *str, int consider_space) 16 | { 17 | int i; 18 | int min; 19 | int max; 20 | 21 | i = -1; 22 | min = 32 + consider_space; 23 | max = 126; 24 | while (str[++i]) 25 | { 26 | if (str[i] >= min && str[i] <= max) 27 | return (0); 28 | } 29 | return (1); 30 | } 31 | -------------------------------------------------------------------------------- /libft/ft_islower.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_islower.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/23 12:45:32 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/23 12:45:33 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_islower(int c) 16 | { 17 | return (c <= 'z' && c >= 'a'); 18 | } 19 | -------------------------------------------------------------------------------- /libft/ft_isprint.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isprint.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/23 21:21:53 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/23 23:40:59 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_isprint(int c) 16 | { 17 | return (c >= 32 && c < 127); 18 | } 19 | -------------------------------------------------------------------------------- /libft/ft_isupper.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isupper.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/23 12:42:09 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/23 12:42:11 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_isupper(int c) 16 | { 17 | return (c <= 'Z' && c >= 'A'); 18 | } 19 | -------------------------------------------------------------------------------- /libft/ft_itoa.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_itoa.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/28 02:04:57 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/28 02:04:59 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | static size_t get_str_len(int n) 16 | { 17 | size_t i; 18 | 19 | i = 1; 20 | while (n /= 10) 21 | i++; 22 | return (i); 23 | } 24 | 25 | char *ft_itoa(int n) 26 | { 27 | char *str; 28 | size_t str_len; 29 | unsigned int n_cpy; 30 | 31 | str_len = get_str_len(n); 32 | n_cpy = n; 33 | if (n < 0) 34 | { 35 | n_cpy = -n; 36 | str_len++; 37 | } 38 | if (!(str = ft_strnew(str_len))) 39 | return (NULL); 40 | str[--str_len] = n_cpy % 10 + '0'; 41 | while (n_cpy /= 10) 42 | str[--str_len] = n_cpy % 10 + '0'; 43 | if (n < 0) 44 | *(str + 0) = '-'; 45 | return (str); 46 | } 47 | -------------------------------------------------------------------------------- /libft/ft_lst_reverse.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lst_reverse.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/10/15 16:09:31 by jrameau #+# #+# */ 9 | /* Updated: 2016/10/15 16:09:37 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | t_list *ft_lst_reverse(t_list *alst) 16 | { 17 | t_list *prev; 18 | t_list *cur; 19 | t_list *next; 20 | 21 | prev = NULL; 22 | cur = alst; 23 | while (cur != NULL) 24 | { 25 | next = cur->next; 26 | cur->next = prev; 27 | prev = cur; 28 | cur = next; 29 | } 30 | alst = prev; 31 | return (alst); 32 | } 33 | -------------------------------------------------------------------------------- /libft/ft_lstadd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstadd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/04/19 22:48:33 by jrameau #+# #+# */ 9 | /* Updated: 2017/04/19 22:48:37 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_lstadd(t_list **alst, t_list *new) 16 | { 17 | new->next = *alst; 18 | *alst = new; 19 | } 20 | -------------------------------------------------------------------------------- /libft/ft_lstaddback.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstaddback.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/29 00:01:01 by jrameau #+# #+# */ 9 | /* Updated: 2017/04/19 22:49:40 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_lstaddback(t_list **alst, t_list *new) 16 | { 17 | t_list *tmp; 18 | t_list *head; 19 | 20 | tmp = *alst; 21 | head = tmp; 22 | while (tmp->next) 23 | tmp = tmp->next; 24 | tmp->next = new; 25 | *alst = head; 26 | } 27 | -------------------------------------------------------------------------------- /libft/ft_lstdel.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstdel.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/28 23:58:22 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/28 23:58:27 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_lstdel(t_list **alst, void (*del)(void *, size_t)) 16 | { 17 | if ((*alst)->next) 18 | ft_lstdel(&(*alst)->next, del); 19 | ft_lstdelone(&(*alst), del); 20 | } 21 | -------------------------------------------------------------------------------- /libft/ft_lstdelone.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstdelone.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/28 23:17:34 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/28 23:17:35 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_lstdelone(t_list **alst, void (*del)(void *, size_t)) 16 | { 17 | del((*alst)->content, (*alst)->content_size); 18 | free(*alst); 19 | *alst = NULL; 20 | } 21 | -------------------------------------------------------------------------------- /libft/ft_lstiter.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstiter.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/29 00:16:06 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/29 00:16:08 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_lstiter(t_list *lst, void (*f)(t_list *elem)) 16 | { 17 | if (!lst) 18 | return ; 19 | if (lst->next) 20 | ft_lstiter(lst->next, f); 21 | f(lst); 22 | } 23 | -------------------------------------------------------------------------------- /libft/ft_lstmap.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstmap.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/29 00:20:40 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/29 00:20:41 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | t_list *ft_lstmap(t_list *lst, t_list *(*f)(t_list *elem)) 16 | { 17 | t_list *new; 18 | t_list *list; 19 | 20 | if (!lst) 21 | return (NULL); 22 | list = f(lst); 23 | new = list; 24 | while (lst->next) 25 | { 26 | lst = lst->next; 27 | if (!(list->next = f(lst))) 28 | { 29 | free(list->next); 30 | return (NULL); 31 | } 32 | list = list->next; 33 | } 34 | return (new); 35 | } 36 | -------------------------------------------------------------------------------- /libft/ft_lstnew.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstnew.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/28 22:49:15 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/28 22:49:16 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | t_list *ft_lstnew(void const *content, size_t content_size) 16 | { 17 | t_list *list; 18 | 19 | if (!(list = (t_list *)malloc(sizeof(*list)))) 20 | return (NULL); 21 | if (!content) 22 | { 23 | list->content = NULL; 24 | list->content_size = 0; 25 | } 26 | else 27 | { 28 | if (!(list->content = malloc(content_size))) 29 | return (NULL); 30 | ft_memcpy(list->content, content, content_size); 31 | list->content_size = content_size; 32 | } 33 | list->next = NULL; 34 | return (list); 35 | } 36 | -------------------------------------------------------------------------------- /libft/ft_memalloc.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memalloc.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/24 03:37:39 by jrameau #+# #+# */ 9 | /* Updated: 2017/05/20 21:44:40 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void *ft_memalloc(size_t size) 16 | { 17 | void *mem; 18 | 19 | if (!(mem = malloc(size))) 20 | return (NULL); 21 | ft_bzero(mem, size); 22 | return (mem); 23 | } 24 | -------------------------------------------------------------------------------- /libft/ft_memccpy.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memccpy.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/21 21:34:36 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/21 21:34:37 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void *ft_memccpy(void *dst, const void *src, int c, size_t n) 16 | { 17 | char *ptr; 18 | size_t i; 19 | 20 | i = -1; 21 | ptr = dst; 22 | while (++i < n) 23 | { 24 | *(ptr + i) = *((unsigned char *)src + i); 25 | if (*((unsigned char *)src + i) == (unsigned char)c) 26 | return (dst + i + 1); 27 | } 28 | return (NULL); 29 | } 30 | -------------------------------------------------------------------------------- /libft/ft_memchr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memchr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/22 13:54:41 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/24 17:55:42 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void *ft_memchr(const void *s, int c, size_t n) 16 | { 17 | const char *sc; 18 | size_t i; 19 | 20 | sc = (const char *)s; 21 | i = -1; 22 | while (++i < n) 23 | if (*(sc + i) == (char)c) 24 | return ((void *)sc + i); 25 | return (NULL); 26 | } 27 | -------------------------------------------------------------------------------- /libft/ft_memcmp.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memcmp.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/22 14:18:12 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/22 14:18:14 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_memcmp(const void *s1, const void *s2, size_t n) 16 | { 17 | unsigned char *s1c; 18 | unsigned char *s2c; 19 | size_t i; 20 | 21 | i = -1; 22 | s1c = (unsigned char *)s1; 23 | s2c = (unsigned char *)s2; 24 | while (++i < n && *(s1c + i) == *(s2c + i)) 25 | ; 26 | if (i == n) 27 | return (0); 28 | return (*(s1c + i) - *(s2c + i)); 29 | } 30 | -------------------------------------------------------------------------------- /libft/ft_memcpy.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memcpy.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/21 16:54:04 by jrameau #+# #+# */ 9 | /* Updated: 2017/05/08 11:58:56 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void *ft_memcpy(void *dst, const void *src, size_t n) 16 | { 17 | size_t i; 18 | 19 | i = -1; 20 | while (++i < n) 21 | ((unsigned char *)dst)[i] = ((unsigned char *)src)[i]; 22 | return (dst); 23 | } 24 | -------------------------------------------------------------------------------- /libft/ft_memdel.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memdel.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/26 20:09:07 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/26 20:09:08 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_memdel(void **ap) 16 | { 17 | if (!ap || !*ap) 18 | return ; 19 | free(*ap); 20 | *ap = 0; 21 | } 22 | -------------------------------------------------------------------------------- /libft/ft_memmove.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memmove.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/22 12:41:41 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/24 17:15:13 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void *ft_memmove(void *dst, const void *src, size_t len) 16 | { 17 | char *srcc; 18 | char *dstc; 19 | size_t i; 20 | 21 | i = -1; 22 | srcc = (char *)src; 23 | dstc = (char *)dst; 24 | if (srcc < dstc) 25 | while ((int)(--len) >= 0) 26 | *(dstc + len) = *(srcc + len); 27 | else 28 | while (++i < len) 29 | *(dstc + i) = *(srcc + i); 30 | return (dst); 31 | } 32 | -------------------------------------------------------------------------------- /libft/ft_memset.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memset.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/21 13:13:13 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/21 13:13:16 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void *ft_memset(void *b, int c, size_t len) 16 | { 17 | char *ptr; 18 | size_t i; 19 | 20 | ptr = b; 21 | i = 0; 22 | while (i < len) 23 | *(ptr + i++) = c; 24 | return (b); 25 | } 26 | -------------------------------------------------------------------------------- /libft/ft_pathjoin.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_pathjoin.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/04/19 22:50:00 by jrameau #+# #+# */ 9 | /* Updated: 2017/05/21 00:39:12 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_pathjoin(char *p1, char *p2) 16 | { 17 | char *tmp2; 18 | 19 | if (!p2 || !p1) 20 | return (NULL); 21 | if (!ft_strendswith(p1, "/")) 22 | { 23 | if (p2[0] == '/') 24 | return (ft_strjoin(p1, p2)); 25 | else 26 | { 27 | tmp2 = ft_strjoincl(ft_strjoinch(p1, '/'), p2, 0); 28 | return (tmp2); 29 | } 30 | } 31 | else 32 | { 33 | if (p2[0] == '/') 34 | return (ft_strjoin(p1, p2 + 1)); 35 | else 36 | return (ft_strjoin(p1, p2)); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /libft/ft_putchar.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putchar.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/08/17 17:45:18 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/28 21:44:04 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_putchar(char c) 16 | { 17 | ft_putchar_fd(c, 1); 18 | } 19 | -------------------------------------------------------------------------------- /libft/ft_putchar_fd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putchar_fd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/28 17:49:11 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/28 17:49:13 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_putchar_fd(char c, int fd) 16 | { 17 | write(fd, &c, 1); 18 | } 19 | -------------------------------------------------------------------------------- /libft/ft_putendl.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putendl.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/28 22:04:25 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/28 22:04:26 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_putendl(char const *s) 16 | { 17 | ft_putendl_fd(s, 1); 18 | } 19 | -------------------------------------------------------------------------------- /libft/ft_putendl_fd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putendl_fd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/28 22:26:34 by jrameau #+# #+# */ 9 | /* Updated: 2017/05/20 22:37:43 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_putendl_fd(char const *s, int fd) 16 | { 17 | char *tmp; 18 | 19 | tmp = ft_strjoin(s, "\n"); 20 | ft_putstr_fd(tmp, fd); 21 | free(tmp); 22 | } 23 | -------------------------------------------------------------------------------- /libft/ft_putnbr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putnbr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/28 22:10:39 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/28 22:10:40 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_putnbr(int n) 16 | { 17 | ft_putnbr_fd(n, 1); 18 | } 19 | -------------------------------------------------------------------------------- /libft/ft_putnbr_fd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putnbr_fd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/28 22:18:35 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/28 22:18:36 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_putnbr_fd(int n, int fd) 16 | { 17 | if (n < 0) 18 | { 19 | ft_putchar_fd('-', fd); 20 | n = -n; 21 | } 22 | if (n == -2147483648) 23 | { 24 | ft_putchar_fd('2', fd); 25 | n %= 1000000000; 26 | n = -n; 27 | } 28 | if (n >= 10) 29 | { 30 | ft_putnbr_fd(n / 10, fd); 31 | ft_putnbr_fd(n % 10, fd); 32 | } 33 | else 34 | ft_putchar_fd(n + '0', fd); 35 | } 36 | -------------------------------------------------------------------------------- /libft/ft_putnstr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putnstr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/04/29 13:50:34 by jrameau #+# #+# */ 9 | /* Updated: 2017/05/21 01:10:08 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_putnstr(char *str, int n) 16 | { 17 | int i; 18 | 19 | i = -1; 20 | if (n < 0) 21 | { 22 | while (str[++i] && i < (int)ft_strlen(str) + n) 23 | ft_putchar(str[i]); 24 | } 25 | else 26 | { 27 | while (str[++i] && i < n) 28 | ft_putchar(str[i]); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /libft/ft_putstr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putstr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/08/17 17:47:31 by jrameau #+# #+# */ 9 | /* Updated: 2016/08/17 17:47:34 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_putstr(char const *s) 16 | { 17 | ft_putstr_fd(s, 1); 18 | } 19 | -------------------------------------------------------------------------------- /libft/ft_putstr_fd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putstr_fd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/28 22:23:42 by jrameau #+# #+# */ 9 | /* Updated: 2017/04/15 18:36:20 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_putstr_fd(char const *s, int fd) 16 | { 17 | write(fd, s, ft_strlen(s)); 18 | } 19 | -------------------------------------------------------------------------------- /libft/ft_realloc.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_realloc.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/10/21 01:11:53 by jrameau #+# #+# */ 9 | /* Updated: 2017/05/20 21:46:33 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void *ft_realloc(void *ptr, size_t prev_size, size_t new_size) 16 | { 17 | void *new; 18 | 19 | if (!ptr) 20 | return (NULL); 21 | if (!(new = ft_memalloc(new_size))) 22 | { 23 | free(ptr); 24 | return (NULL); 25 | } 26 | ft_memcpy(new, ptr, prev_size < new_size ? prev_size : new_size); 27 | free(ptr); 28 | return (new); 29 | } 30 | -------------------------------------------------------------------------------- /libft/ft_strarrmax.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strarrmax.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/06/27 22:04:11 by jrameau #+# #+# */ 9 | /* Updated: 2017/07/02 17:56:40 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include 14 | 15 | int ft_strarrmax(char **arr) 16 | { 17 | int i; 18 | int max; 19 | int curr_len; 20 | 21 | i = -1; 22 | max = 0; 23 | while (arr[++i]) 24 | { 25 | curr_len = ft_strlen(arr[i]); 26 | if (curr_len > max) 27 | max = curr_len; 28 | } 29 | return (max); 30 | } 31 | -------------------------------------------------------------------------------- /libft/ft_strcat.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strcat.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/22 18:07:43 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/22 18:07:44 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strcat(char *s1, const char *s2) 16 | { 17 | int i; 18 | int j; 19 | 20 | i = -1; 21 | j = (int)ft_strlen(s1); 22 | while (*(s2 + ++i)) 23 | *(s1 + j++) = *(s2 + i); 24 | *(s1 + j) = '\0'; 25 | return (s1); 26 | } 27 | -------------------------------------------------------------------------------- /libft/ft_strchr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strchr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/23 00:41:20 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/23 00:41:21 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strchr(const char *s, int c) 16 | { 17 | int i; 18 | 19 | i = -1; 20 | while (++i < (int)ft_strlen(s) + 1) 21 | if (*(s + i) == (char)c) 22 | return ((char *)s + i); 23 | return (NULL); 24 | } 25 | -------------------------------------------------------------------------------- /libft/ft_strclr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strclr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/26 23:07:51 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/26 23:07:53 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_strclr(char *s) 16 | { 17 | if (s) 18 | ft_bzero(s, ft_strlen(s)); 19 | } 20 | -------------------------------------------------------------------------------- /libft/ft_strcmp.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strcmp.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/23 03:51:36 by jrameau #+# #+# */ 9 | /* Updated: 2017/02/21 15:53:57 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_strcmp(const char *s1, const char *s2) 16 | { 17 | int i; 18 | 19 | i = 0; 20 | while (*(s1 + i) && *(s1 + i) == *(s2 + i)) 21 | i++; 22 | return (*((unsigned char *)s1 + i) - *((unsigned char *)s2 + i)); 23 | } 24 | -------------------------------------------------------------------------------- /libft/ft_strcpy.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strcpy.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/22 15:25:07 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/24 02:49:18 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strcpy(char *dst, const char *src) 16 | { 17 | int i; 18 | 19 | i = -1; 20 | while (*(src + ++i)) 21 | *(dst + i) = *(src + i); 22 | *(dst + i) = '\0'; 23 | return (dst); 24 | } 25 | -------------------------------------------------------------------------------- /libft/ft_strdel.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strdel.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/26 23:03:47 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/26 23:03:48 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_strdel(char **as) 16 | { 17 | if (!as || !*as) 18 | return ; 19 | free(*as); 20 | *as = 0; 21 | } 22 | -------------------------------------------------------------------------------- /libft/ft_strdup.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strdup.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/22 14:52:14 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/22 14:52:15 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strdup(const char *s1) 16 | { 17 | return (ft_strndup(s1, ft_strlen(s1))); 18 | } 19 | -------------------------------------------------------------------------------- /libft/ft_strendswith.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strendswith.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/01/17 20:56:49 by jrameau #+# #+# */ 9 | /* Updated: 2017/04/19 22:54:50 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_strendswith(char *s1, char *s2) 16 | { 17 | int i; 18 | 19 | i = -1; 20 | while (s1[++i]) 21 | if (s1[i] == s2[0]) 22 | if (ft_strcmp(s1 + i, s2) == 0) 23 | return (1); 24 | return (0); 25 | } 26 | -------------------------------------------------------------------------------- /libft/ft_strequ.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strequ.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/26 23:42:03 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/26 23:42:04 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_strequ(char const *s1, char const *s2) 16 | { 17 | if (!s1 || !s2) 18 | return (0); 19 | return (ft_strcmp(s1, s2) ? 0 : 1); 20 | } 21 | -------------------------------------------------------------------------------- /libft/ft_striter.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_striter.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/26 23:12:58 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/26 23:12:59 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_striter(char *s, void (*f)(char *)) 16 | { 17 | int i; 18 | 19 | i = 0; 20 | if (s && f) 21 | while (*(s + i)) 22 | f(s + i++); 23 | } 24 | -------------------------------------------------------------------------------- /libft/ft_striteri.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_striteri.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/26 23:22:13 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/26 23:22:15 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_striteri(char *s, void (*f)(unsigned int, char *)) 16 | { 17 | int i; 18 | 19 | i = -1; 20 | if (s && f) 21 | while (*(s + ++i)) 22 | f(i, s + i); 23 | } 24 | -------------------------------------------------------------------------------- /libft/ft_strjoin.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strjoin.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/27 00:08:20 by jrameau #+# #+# */ 9 | /* Updated: 2017/05/18 20:52:57 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strjoin(char const *s1, char const *s2) 16 | { 17 | char *new_str; 18 | size_t i; 19 | size_t j; 20 | size_t s1_len; 21 | size_t s2_len; 22 | 23 | if (!s1) 24 | return (NULL); 25 | if (!s2) 26 | return (ft_strdup(s1)); 27 | s1_len = ft_strlen(s1); 28 | s2_len = ft_strlen(s2); 29 | new_str = ft_strnew(s1_len + s2_len + 1); 30 | if (!new_str) 31 | return (NULL); 32 | i = -1; 33 | j = -1; 34 | while (++i < s1_len) 35 | *(new_str + i) = *(s1 + i); 36 | while (++j < s2_len) 37 | *(new_str + i++) = *(s2 + j); 38 | return (new_str); 39 | } 40 | -------------------------------------------------------------------------------- /libft/ft_strjoinch.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strjoin.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/27 00:08:20 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/27 00:08:21 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strjoinch(char const *s1, char c) 16 | { 17 | char *new_str; 18 | size_t i; 19 | size_t s1_len; 20 | 21 | if (!s1 || !c) 22 | return (NULL); 23 | s1_len = ft_strlen(s1); 24 | new_str = ft_strnew(s1_len + 1); 25 | if (!new_str) 26 | return (NULL); 27 | i = -1; 28 | while (++i < s1_len) 29 | *(new_str + i) = *(s1 + i); 30 | *(new_str + i) = c; 31 | return (new_str); 32 | } 33 | -------------------------------------------------------------------------------- /libft/ft_strjoinchcl.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strjoinchcl.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/05/18 19:30:53 by jrameau #+# #+# */ 9 | /* Updated: 2017/05/19 20:57:44 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strjoinchcl(char *s1, char c) 16 | { 17 | char *new; 18 | 19 | if (!(new = ft_strjoinch(s1, c))) 20 | return (NULL); 21 | free(s1); 22 | s1 = NULL; 23 | return (new); 24 | } 25 | -------------------------------------------------------------------------------- /libft/ft_strjoincl.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strjoincl.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/05/18 19:30:53 by jrameau #+# #+# */ 9 | /* Updated: 2017/05/19 10:59:24 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strjoincl(char *s1, char *s2, int free_both) 16 | { 17 | char *new; 18 | 19 | if (!(new = ft_strjoin(s1, s2))) 20 | return (NULL); 21 | free(s1); 22 | s1 = NULL; 23 | if (free_both) 24 | { 25 | free(s2); 26 | s2 = NULL; 27 | } 28 | return (new); 29 | } 30 | -------------------------------------------------------------------------------- /libft/ft_strlcat.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strlcat.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/22 19:27:25 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/24 22:41:08 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | size_t ft_strlcat(char *dst, const char *src, size_t size) 16 | { 17 | size_t i; 18 | int j; 19 | size_t dst_len; 20 | size_t src_len; 21 | 22 | i = ft_strlen(dst); 23 | j = 0; 24 | dst_len = ft_strlen(dst); 25 | src_len = ft_strlen(src); 26 | if (size < dst_len + 1) 27 | return (src_len + size); 28 | if (size > dst_len + 1) 29 | { 30 | while (i < size - 1) 31 | *(dst + i++) = *(src + j++); 32 | *(dst + i) = '\0'; 33 | } 34 | return (dst_len + src_len); 35 | } 36 | -------------------------------------------------------------------------------- /libft/ft_strlen.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strlen.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/22 12:43:37 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/22 12:43:39 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | size_t ft_strlen(const char *s) 16 | { 17 | size_t i; 18 | 19 | i = -1; 20 | while (*(s + ++i)) 21 | ; 22 | return (i); 23 | } 24 | -------------------------------------------------------------------------------- /libft/ft_strmap.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strmap.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/26 23:27:07 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/26 23:27:08 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strmap(char const *s, char (*f)(char)) 16 | { 17 | char *new_str; 18 | int i; 19 | 20 | if (!s) 21 | return (NULL); 22 | new_str = ft_strnew(ft_strlen(s)); 23 | if (!new_str) 24 | return (NULL); 25 | i = -1; 26 | while (*(s + ++i)) 27 | *(new_str + i) = f(*(s + i)); 28 | return (new_str); 29 | } 30 | -------------------------------------------------------------------------------- /libft/ft_strmapi.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strmapi.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/26 23:39:35 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/26 23:39:43 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strmapi(char const *s, char (*f)(unsigned int, char)) 16 | { 17 | char *new_str; 18 | int i; 19 | 20 | if (!s) 21 | return (NULL); 22 | new_str = ft_strnew(ft_strlen(s)); 23 | if (!new_str) 24 | return (NULL); 25 | i = -1; 26 | while (*(s + ++i)) 27 | *(new_str + i) = f(i, *(s + i)); 28 | return (new_str); 29 | } 30 | -------------------------------------------------------------------------------- /libft/ft_strncat.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strncat.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/22 18:58:25 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/24 03:16:42 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strncat(char *s1, const char *s2, size_t n) 16 | { 17 | int i; 18 | int j; 19 | 20 | i = -1; 21 | j = (int)ft_strlen(s1); 22 | while (*(s2 + ++i) && i < (int)n) 23 | *(s1 + j++) = *(s2 + i); 24 | *(s1 + j) = '\0'; 25 | return (s1); 26 | } 27 | -------------------------------------------------------------------------------- /libft/ft_strnchr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strnchr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/11/26 05:38:48 by jrameau #+# #+# */ 9 | /* Updated: 2016/11/26 05:39:55 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strnchr(char *s, char c, int offset) 16 | { 17 | int i; 18 | 19 | i = -1; 20 | while (s[++i]) 21 | if (s[i] == c) 22 | return (s + i + offset); 23 | return (NULL); 24 | } 25 | -------------------------------------------------------------------------------- /libft/ft_strncmp.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strncmp.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/23 04:22:10 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/23 04:22:11 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_strncmp(const char *s1, const char *s2, size_t n) 16 | { 17 | int i; 18 | 19 | i = 0; 20 | while (*(s1 + i) && *(s1 + i) == *(s2 + i) && i < (int)n - 1) 21 | i++; 22 | if (n) 23 | return (*((unsigned char *)s1 + i) - *((unsigned char *)s2 + i)); 24 | return (0); 25 | } 26 | -------------------------------------------------------------------------------- /libft/ft_strncpy.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strncpy.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/24 02:48:43 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/24 03:25:13 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strncpy(char *dst, const char *src, size_t len) 16 | { 17 | size_t i; 18 | 19 | i = -1; 20 | while (++i < len) 21 | if (*(src + i)) 22 | *(dst + i) = *(src + i); 23 | else 24 | while (i < len) 25 | *(dst + i++) = '\0'; 26 | return (dst); 27 | } 28 | -------------------------------------------------------------------------------- /libft/ft_strndup.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strndup.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/29 15:20:20 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/29 15:20:23 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strndup(const char *s1, size_t n) 16 | { 17 | char *tmp; 18 | 19 | if (!(tmp = ft_strnew(n))) 20 | return (NULL); 21 | ft_strncpy(tmp, s1, n); 22 | return (tmp); 23 | } 24 | -------------------------------------------------------------------------------- /libft/ft_strnequ.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strnequ.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/26 23:47:31 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/26 23:47:36 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_strnequ(char const *s1, char const *s2, size_t n) 16 | { 17 | if (!s1 || !s2) 18 | return (0); 19 | return (ft_strncmp(s1, s2, n) ? 0 : 1); 20 | } 21 | -------------------------------------------------------------------------------- /libft/ft_strnew.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strnew.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/26 22:53:09 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/26 22:53:10 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strnew(size_t size) 16 | { 17 | char *str; 18 | 19 | str = (char *)malloc(sizeof(char) * size + 1); 20 | if (!str) 21 | return (NULL); 22 | ft_bzero(str, size + 1); 23 | return (str); 24 | } 25 | -------------------------------------------------------------------------------- /libft/ft_strnstr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strnstr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/23 03:19:21 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/25 02:15:51 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strnstr(const char *big, const char *little, size_t len) 16 | { 17 | size_t i; 18 | size_t j; 19 | size_t k; 20 | int found; 21 | 22 | i = -1; 23 | found = 1; 24 | if (!ft_strlen(little)) 25 | return ((char *)big); 26 | while (*(big + ++i) && i < len) 27 | { 28 | j = 0; 29 | if (*(big + i) == *(little + 0)) 30 | { 31 | k = i; 32 | found = 1; 33 | while (*(big + k) && *(little + j) && j < len && k < len) 34 | if (*(big + k++) != *(little + j++)) 35 | found = 0; 36 | if (found && !*(little + j)) 37 | return ((char *)big + i); 38 | } 39 | } 40 | return (NULL); 41 | } 42 | -------------------------------------------------------------------------------- /libft/ft_strpop.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strpop.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/05/12 17:01:31 by jrameau #+# #+# */ 9 | /* Updated: 2017/05/21 01:11:26 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strpop(char *str) 16 | { 17 | char *new; 18 | 19 | new = ft_strndup(str, ft_strlen(str) - 1); 20 | free(str); 21 | return (new); 22 | } 23 | -------------------------------------------------------------------------------- /libft/ft_strrchr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strrchr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/29 15:25:57 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/29 15:25:59 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strrchr(const char *s, int c) 16 | { 17 | int i; 18 | 19 | i = (int)ft_strlen(s) + 1; 20 | while (i--) 21 | if (*(s + i) == (char)c) 22 | return ((char *)s + i); 23 | return (NULL); 24 | } 25 | -------------------------------------------------------------------------------- /libft/ft_strreplace.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strreplace.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/05/09 20:14:03 by jrameau #+# #+# */ 9 | /* Updated: 2017/05/21 01:15:53 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strreplace(char *str, char *term, char *replace_by) 16 | { 17 | int i; 18 | char *new_path; 19 | int done; 20 | 21 | if (!ft_strstr(str, term)) 22 | return (NULL); 23 | new_path = ft_strnew(1); 24 | i = -1; 25 | done = 0; 26 | while (str[++i]) 27 | { 28 | if (ft_strstartswith(str + i, term) && !done) 29 | { 30 | new_path = ft_strjoincl(new_path, replace_by, 0); 31 | i += ft_strlen(term); 32 | if (!str[i]) 33 | break ; 34 | new_path = ft_strjoinchcl(new_path, str[i]); 35 | done = 1; 36 | } 37 | else 38 | new_path = ft_strjoinchcl(new_path, str[i]); 39 | } 40 | return (new_path); 41 | } 42 | -------------------------------------------------------------------------------- /libft/ft_strsplit.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strsplit.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/27 13:18:35 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/27 13:18:36 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | static int get_word_len(char const *str, char c) 16 | { 17 | int i; 18 | int len; 19 | 20 | i = 0; 21 | len = 0; 22 | while (str[i] == c) 23 | i++; 24 | while (str[i] != c && str[i] != '\0') 25 | { 26 | i++; 27 | len++; 28 | } 29 | return (len); 30 | } 31 | 32 | char **ft_strsplit(char const *s, char c) 33 | { 34 | int i; 35 | int j; 36 | int k; 37 | char **str2; 38 | 39 | if (!s || !(str2 = (char **)malloc(sizeof(*str2) * 40 | (ft_countwords(s, c) + 1)))) 41 | return (NULL); 42 | i = -1; 43 | j = 0; 44 | while (++i < ft_countwords(s, c)) 45 | { 46 | k = 0; 47 | if (!(str2[i] = ft_strnew(get_word_len(&s[j], c) + 1))) 48 | str2[i] = NULL; 49 | while (s[j] == c) 50 | j++; 51 | while (s[j] != c && s[j]) 52 | str2[i][k++] = s[j++]; 53 | str2[i][k] = '\0'; 54 | } 55 | str2[i] = 0; 56 | return (str2); 57 | } 58 | -------------------------------------------------------------------------------- /libft/ft_strsplitall.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strsplitall.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/27 13:18:35 by jrameau #+# #+# */ 9 | /* Updated: 2017/05/11 00:24:28 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | static int get_word_len(char const *str) 16 | { 17 | int i; 18 | int len; 19 | 20 | i = 0; 21 | len = 0; 22 | while (IS_SPACE(str[i])) 23 | i++; 24 | while (!IS_SPACE(str[i]) && str[i] != '\0') 25 | { 26 | i++; 27 | len++; 28 | } 29 | return (len); 30 | } 31 | 32 | char **ft_strsplitall(char const *s) 33 | { 34 | int i; 35 | int j; 36 | int k; 37 | char **str2; 38 | 39 | if (!s || !(str2 = (char **)malloc(sizeof(*str2) * 40 | (ft_countwordsall(s) + 1)))) 41 | return (NULL); 42 | i = -1; 43 | j = 0; 44 | while (++i < ft_countwordsall(s)) 45 | { 46 | k = 0; 47 | if (!(str2[i] = ft_strnew(get_word_len(&s[j]) + 1))) 48 | str2[i] = NULL; 49 | while (IS_SPACE(s[j])) 50 | j++; 51 | while (!IS_SPACE(s[j]) && s[j]) 52 | str2[i][k++] = s[j++]; 53 | str2[i][k] = '\0'; 54 | } 55 | str2[i] = 0; 56 | return (str2); 57 | } 58 | -------------------------------------------------------------------------------- /libft/ft_strstartswith.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strstartswith.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/12/14 17:33:27 by jrameau #+# #+# */ 9 | /* Updated: 2017/04/19 22:52:24 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | int ft_strstartswith(char *s1, char *s2) 14 | { 15 | int i; 16 | 17 | i = -1; 18 | while (s2[++i]) 19 | if (s1[i] != s2[i]) 20 | return (0); 21 | return (1); 22 | } 23 | -------------------------------------------------------------------------------- /libft/ft_strstr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strstr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/23 01:39:09 by jrameau #+# #+# */ 9 | /* Updated: 2017/01/17 22:39:47 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strstr(const char *big, const char *little) 16 | { 17 | int i; 18 | int j; 19 | int k; 20 | int good; 21 | 22 | if (!ft_strlen(little)) 23 | return ((char *)big); 24 | i = -1; 25 | good = 0; 26 | while (*(big + ++i) && !good) 27 | { 28 | if (*(big + i) == *(little + 0)) 29 | { 30 | j = 0; 31 | k = i; 32 | good = 1; 33 | while (*(little + j)) 34 | if (*(little + j++) != *(big + k++)) 35 | good = 0; 36 | if (good) 37 | return ((char *)big + i); 38 | } 39 | } 40 | return (NULL); 41 | } 42 | -------------------------------------------------------------------------------- /libft/ft_strsub.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strsub.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/26 23:50:07 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/26 23:50:08 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strsub(char const *s, unsigned int start, size_t len) 16 | { 17 | char *new_str; 18 | size_t i; 19 | 20 | if (!s) 21 | return (NULL); 22 | new_str = ft_strnew(len); 23 | if (!new_str) 24 | return (NULL); 25 | i = 0; 26 | while (i < len) 27 | *(new_str + i++) = *(s + start++); 28 | return (new_str); 29 | } 30 | -------------------------------------------------------------------------------- /libft/ft_strtrim.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* strtrim.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/27 00:50:46 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/27 00:50:47 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | static int has_whitespaces(char *str, int *i, size_t *j) 16 | { 17 | while (IS_SPACE(*(str + *i))) 18 | (*i)++; 19 | while (IS_SPACE(*(str + *j))) 20 | (*j)--; 21 | if (*i || *j < ft_strlen(str)) 22 | return (1); 23 | return (0); 24 | } 25 | 26 | char *ft_strtrim(char const *s) 27 | { 28 | int i; 29 | size_t j; 30 | int k; 31 | char *new_str; 32 | size_t new_size; 33 | 34 | if (!s) 35 | return (NULL); 36 | i = 0; 37 | k = 0; 38 | j = ft_strlen(s) - 1; 39 | if (!has_whitespaces((char *)s, &i, &j) || !ft_strlen(s)) 40 | return ((char *)s); 41 | new_size = (i == (int)ft_strlen(s)) ? 0 : ft_strlen(s) - (size_t)i - \ 42 | (ft_strlen(s) - j); 43 | new_str = ft_strnew(new_size + 1); 44 | if (!new_str) 45 | return (NULL); 46 | while (i <= (int)j) 47 | *(new_str + k++) = *(s + i++); 48 | return (new_str); 49 | } 50 | -------------------------------------------------------------------------------- /libft/ft_tolower.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_tolower.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/23 23:44:33 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/24 03:24:45 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_tolower(int c) 16 | { 17 | if (ft_isupper(c)) 18 | return (c + 32); 19 | return (c); 20 | } 21 | -------------------------------------------------------------------------------- /libft/ft_toupper.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_toupper.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/23 22:57:01 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/24 03:25:25 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_toupper(int c) 16 | { 17 | if (ft_islower(c)) 18 | return (c - 32); 19 | return (c); 20 | } 21 | -------------------------------------------------------------------------------- /libft/get_next_line.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* get_next_line.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/29 22:52:30 by jrameau #+# #+# */ 9 | /* Updated: 2017/04/24 19:07:49 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | static t_list *get_correct_file(t_list **file, int fd) 16 | { 17 | t_list *tmp; 18 | 19 | tmp = *file; 20 | while (tmp) 21 | { 22 | if ((int)tmp->content_size == fd) 23 | return (tmp); 24 | tmp = tmp->next; 25 | } 26 | tmp = ft_lstnew("\0", fd); 27 | ft_lstadd(file, tmp); 28 | tmp = *file; 29 | return (tmp); 30 | } 31 | 32 | int get_next_line(const int fd, char **line) 33 | { 34 | char buf[BUFF_SIZE + 1]; 35 | static t_list *file; 36 | int i; 37 | int ret; 38 | t_list *curr; 39 | 40 | if ((fd < 0 || line == NULL || read(fd, buf, 0) < 0)) 41 | return (-1); 42 | curr = get_correct_file(&file, fd); 43 | MALLCHECK((*line = ft_strnew(1))); 44 | while ((ret = read(fd, buf, BUFF_SIZE))) 45 | { 46 | buf[ret] = '\0'; 47 | MALLCHECK((curr->content = ft_strjoin(curr->content, buf))); 48 | if (ft_strchr(buf, '\n')) 49 | break ; 50 | } 51 | if (ret < BUFF_SIZE && !ft_strlen(curr->content)) 52 | return (0); 53 | i = ft_copyuntil(line, curr->content, '\n'); 54 | (i < (int)ft_strlen(curr->content)) 55 | ? curr->content += (i + 1) 56 | : ft_strclr(curr->content); 57 | return (1); 58 | } 59 | -------------------------------------------------------------------------------- /libft/includes/libft.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* libft.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/21 21:30:24 by jrameau #+# #+# */ 9 | /* Updated: 2017/07/01 16:44:48 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef LIBFT_H 14 | # define LIBFT_H 15 | # include 16 | # include 17 | # include 18 | 19 | # define BUFF_SIZE 100 20 | # define MALLCHECK(x) if (!x) return (-1); 21 | # define IS_SPACE(x) (x == ' ' || x == '\t' || x == '\r' || x == '\f') 22 | 23 | void ft_putchar(char c); 24 | void ft_putstr(char const *str); 25 | void ft_putendl(char const *str); 26 | void ft_putnbr(int nbr); 27 | void ft_putchar_fd(char c, int fd); 28 | void ft_putstr_fd(char const *s, int fd); 29 | void ft_putendl_fd(char const *s, int fd); 30 | void ft_putnbr_fd(int n, int fd); 31 | 32 | char *ft_itoa(int n); 33 | int ft_atoi(const char *str); 34 | 35 | void *ft_memalloc(size_t size); 36 | void ft_memdel(void **ap); 37 | char *ft_strnew(size_t size); 38 | void ft_strdel(char **as); 39 | void ft_strclr(char *s); 40 | void ft_striter(char *s, void (*f)(char *)); 41 | void ft_striteri(char *s, void (*f)(unsigned int, char *)); 42 | char *ft_strmap(char const *s, char (*f)(char)); 43 | char *ft_strmapi(char const *s, char (*f)(unsigned int, char)); 44 | 45 | int ft_strequ(char const *s1, char const *s2); 46 | int ft_strnequ(char const *s1, char const *s2, size_t n); 47 | char *ft_strsub(char const *s, unsigned int start, size_t len); 48 | char *ft_strjoin(char const *s1, char const *s2); 49 | char *ft_strtrim(char const *s); 50 | char **ft_strsplit(char const *s, char c); 51 | 52 | void *ft_memset(void *b, int c, size_t len); 53 | void ft_bzero(void *s, size_t n); 54 | void *ft_memcpy(void *dst, const void *src, size_t n); 55 | void *ft_memccpy(void *dst, const void *restrict src, 56 | int c, size_t n); 57 | void *ft_memmove(void *dst, const void *src, size_t len); 58 | void *ft_memchr(const void *s, int c, size_t n); 59 | int ft_memcmp(const void *s1, const void *s2, size_t n); 60 | 61 | size_t ft_strlen(const char *str); 62 | char *ft_strdup(const char *s1); 63 | char *ft_strndup(const char *s1, size_t n); 64 | char *ft_strcpy(char *dst, const char *src); 65 | char *ft_strncpy(char *dst, const char *src, size_t len); 66 | char *ft_strcat(char *s1, const char *s2); 67 | char *ft_strncat(char *s1, const char *s2, size_t n); 68 | size_t ft_strlcat(char *dst, const char *src, size_t size); 69 | char *ft_strchr(const char *s, int c); 70 | char *ft_strrchr(const char *s, int c); 71 | char *ft_strstr(const char *big, const char *little); 72 | char *ft_strnstr(const char *big, 73 | const char *little, size_t len); 74 | int ft_strcmp(const char *s1, const char *s2); 75 | int ft_strncmp(const char *s1, const char *s2, size_t n); 76 | 77 | int ft_isalpha(int c); 78 | int ft_isdigit(int c); 79 | int ft_isalnum(int c); 80 | int ft_isascii(int c); 81 | int ft_isprint(int c); 82 | int ft_toupper(int c); 83 | int ft_tolower(int c); 84 | 85 | # ifndef IS_SPACE 86 | # define IS_SPACE(x) (x==' '||x=='\n'||x=='\t') 87 | # endif 88 | 89 | typedef struct s_list 90 | { 91 | void *content; 92 | size_t content_size; 93 | struct s_list *next; 94 | } t_list; 95 | 96 | t_list *ft_lstnew(const void *content, size_t content_size); 97 | void ft_lstdelone(t_list **alst, void (*del)(void *, size_t)); 98 | void ft_lstdel(t_list **alst, void (*del)(void *, size_t)); 99 | void ft_lstadd(t_list **alst, t_list *n); 100 | void ft_lstiter(t_list *lst, void (*f)(t_list *elem)); 101 | t_list *ft_lstmap(t_list *lst, t_list *(*f)(t_list *elem)); 102 | 103 | /* 104 | ** Extra functions 105 | */ 106 | 107 | int ft_isupper(int c); 108 | int ft_islower(int c); 109 | int ft_countwords(char const *str, char c); 110 | char *ft_strndup(const char *s1, size_t n); 111 | char *ft_capitalize(char *s); 112 | t_list *ft_lst_reverse(t_list *alst); 113 | void *ft_realloc(void *ptr, size_t prev_size, size_t new_size); 114 | char *ft_strjoinch(char const *s1, char c); 115 | char *ft_strnchr(char *s, char c, int offset); 116 | int ft_copyuntil(char **dst, char *src, char c); 117 | int ft_strstartswith(char *s1, char *s2); 118 | int ft_intlen(int num); 119 | int ft_strendswith(char *s1, char *s2); 120 | char *ft_pathjoin(char *p1, char *p2); 121 | void ft_lstaddback(t_list **alst, t_list *new); 122 | int get_next_line(const int fd, char **line); 123 | void ft_putnstr(char *str, int n); 124 | char *ft_strreplace(char *str, char *term, char *replace_by); 125 | int ft_isemptystr(char *str, int consider_space); 126 | char **ft_strsplitall(char const *s); 127 | int ft_countwordsall(char const *str); 128 | void ft_freestrarr(char **arr); 129 | char *ft_strjoincl(char *s1, char *s2, int free_both); 130 | char *ft_strjoinchcl(char *s1, char c); 131 | int ft_count2darray(char **arr); 132 | int ft_strarrmax(char **arr); 133 | char *ft_get_parent_path(char *path); 134 | 135 | #endif 136 | -------------------------------------------------------------------------------- /norme.en.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickdotht/ft_select/054e83def5eed7b379ace22c250d4744b9450a4a/norme.en.pdf -------------------------------------------------------------------------------- /src/actions.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* actions.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/06/28 10:43:41 by jrameau #+# #+# */ 9 | /* Updated: 2017/07/02 18:18:38 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include 14 | 15 | /* 16 | ** Moves up the argument list 17 | ** 18 | ** @param N/A 19 | ** @return N/A 20 | */ 21 | 22 | static void move_up(void) 23 | { 24 | int i; 25 | t_arg *tmp; 26 | int cols; 27 | 28 | i = 0; 29 | tmp = *g_select.active_arg; 30 | cols = count_columns(); 31 | while (++i < cols) 32 | tmp = tmp->prev; 33 | g_select.active_arg = &tmp->prev; 34 | } 35 | 36 | /* 37 | ** Moves down the argument list 38 | ** 39 | ** @param N/A 40 | ** @return N/A 41 | */ 42 | 43 | static void move_down(void) 44 | { 45 | int i; 46 | t_arg *tmp; 47 | int cols; 48 | 49 | i = 0; 50 | tmp = *g_select.active_arg; 51 | cols = count_columns(); 52 | while (++i < cols) 53 | tmp = tmp->next; 54 | g_select.active_arg = &tmp->next; 55 | } 56 | 57 | /* 58 | ** Provides argument browsing capabilities using the arrow keys 59 | ** 60 | ** @param direction the direction in which to go 61 | ** @return N/A 62 | */ 63 | 64 | void move(t_dir direction) 65 | { 66 | t_arg *active; 67 | 68 | active = *g_select.active_arg; 69 | if (direction == RIGHT_DIR) 70 | g_select.active_arg = &active->next; 71 | else if (direction == LEFT_DIR) 72 | g_select.active_arg = &active->prev; 73 | else if (direction == UP_DIR) 74 | move_up(); 75 | else if (direction == DOWN_DIR) 76 | move_down(); 77 | } 78 | -------------------------------------------------------------------------------- /src/column_display.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* column_display.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/06/27 21:39:08 by jrameau #+# #+# */ 9 | /* Updated: 2017/07/03 02:47:25 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include 14 | 15 | /* 16 | ** Returns either the current width or height of the terminal depending on the 17 | ** value of 'w_or_h' 18 | ** 19 | ** @param w_or_h 1 for width, 0 for height 20 | ** @return The value of the width/height 21 | */ 22 | 23 | static int get_term_size(int w_or_h) 24 | { 25 | struct winsize w; 26 | 27 | ioctl(STDERR_FILENO, TIOCGWINSZ, &w); 28 | return ((w_or_h) ? w.ws_col : w.ws_row); 29 | } 30 | 31 | /* 32 | ** Counts the maximum amount of columns possible on the current terminal size 33 | ** 34 | ** @param N/A 35 | ** @return The total amount of columns possible 36 | */ 37 | 38 | int count_columns(void) 39 | { 40 | int cols; 41 | 42 | cols = get_term_size(1) / (count_max_arg_len() + 1); 43 | if (!cols) 44 | cols = 1; 45 | if ((count_max_arg_len() + 1) * g_select.argc < get_term_size(1)) 46 | cols = g_select.argc; 47 | return (cols); 48 | } 49 | 50 | /* 51 | ** Prints the value of the passed in argument in the desired fd 52 | ** 53 | ** Depending on the type of the argument, we print it in a specific color 54 | ** and then set it back to the default color. 55 | ** 56 | ** @param arg The argument to print the value of 57 | ** @param fd The file descriptor in which to print to 58 | ** @return N/A 59 | */ 60 | 61 | void print_value_fd(t_arg *arg, int fd) 62 | { 63 | if (arg->type == C_T && fd == STDERR_FILENO) 64 | ft_putstr_fd(C_COLOR, fd); 65 | else if (arg->type == O_T && fd == STDERR_FILENO) 66 | ft_putstr_fd(O_COLOR, fd); 67 | else if (arg->type == H_T && fd == STDERR_FILENO) 68 | ft_putstr_fd(H_COLOR, fd); 69 | else if (arg->type == MAKEFILE_T && fd == STDERR_FILENO) 70 | ft_putstr_fd(MAKEFILE_COLOR, fd); 71 | else if (arg->type == DOT_T && fd == STDERR_FILENO) 72 | ft_putstr_fd(DOT_COLOR, fd); 73 | else if (arg->type == A_T && fd == STDERR_FILENO) 74 | ft_putstr_fd(A_COLOR, fd); 75 | ft_putstr_fd(arg->value, fd); 76 | if (fd == STDERR_FILENO) 77 | ft_putstr_fd(DEFAULT_COLOR, fd); 78 | } 79 | 80 | /* 81 | ** Displays the list of arguments on the screen 82 | ** 83 | ** Depending on the type of the value and the state in which it is, it will: 84 | ** - underline the text if it's the active value 85 | ** - reverse video the text if the value is selected 86 | ** and then it prints the value 87 | ** 88 | ** @param args The list of arguments 89 | ** @param first The first element of the list, passed as an argument to 90 | ** save lines in the function (25 lines limit) 91 | ** @param rows The amount of rows possible 92 | ** @param cols The amount of columns possible 93 | ** @return N/A 94 | */ 95 | 96 | static void display_args(t_arg *args, t_arg *first, int rows, int cols) 97 | { 98 | int i; 99 | int j; 100 | int str_len; 101 | 102 | i = -1; 103 | while (++i < rows) 104 | { 105 | j = -1; 106 | while (++j < cols) 107 | { 108 | if (args == (*g_select.active_arg)) 109 | ft_putstr_fd(UNDERLINED, STDERR_FILENO); 110 | if (args->is_selected) 111 | ft_putstr_fd(REVERSE_VIDEO_COLOR, STDERR_FILENO); 112 | print_value_fd(args, STDERR_FILENO); 113 | str_len = ft_strlen(args->value); 114 | while (str_len++ <= count_max_arg_len()) 115 | ft_putstr_fd(" ", STDERR_FILENO); 116 | if (args->next == first) 117 | break ; 118 | args = args->next; 119 | } 120 | ft_putstr_fd("\n", STDERR_FILENO); 121 | } 122 | } 123 | 124 | /* 125 | ** Displays the columns of values 126 | ** 127 | ** If it cannot fit at least 1 column on the screen, it displays 128 | ** a blank screen 129 | ** If it can, it first clears the whole screen using the "cl" id of the 130 | ** terminfo database, calculates rows and columns. 131 | ** It also displays a blank screen if the height of the window is not long 132 | ** enough. Then it displays thre list of arguments. 133 | ** 134 | ** @param N/A 135 | ** @return N/A 136 | */ 137 | 138 | void column_display(void) 139 | { 140 | int cols; 141 | int rows; 142 | 143 | if (!g_select.args || count_max_arg_len() > get_term_size(1)) 144 | return ; 145 | tputs(tgetstr("cl", NULL), 1, ft_printnbr); 146 | cols = count_columns(); 147 | rows = g_select.argc / cols; 148 | if (rows > get_term_size(0)) 149 | return ; 150 | if (g_select.argc % cols) 151 | ++rows; 152 | display_args(g_select.args, g_select.args, rows, cols); 153 | } 154 | -------------------------------------------------------------------------------- /src/init_args.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* init_args.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/06/27 23:19:20 by jrameau #+# #+# */ 9 | /* Updated: 2017/07/03 02:31:06 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include 14 | 15 | /* 16 | ** Frees every allocated memory from the global variable 'g_select' 17 | ** 18 | ** @param N/A 19 | ** @return N/A 20 | */ 21 | 22 | void free_args(void) 23 | { 24 | t_arg *first; 25 | t_arg *curr; 26 | 27 | first = g_select.args; 28 | while (g_select.args) 29 | { 30 | curr = g_select.args; 31 | free(g_select.args->value); 32 | g_select.args->value = NULL; 33 | g_select.argc--; 34 | if (g_select.args->next == first) 35 | break ; 36 | g_select.args = g_select.args->next; 37 | free(curr); 38 | curr = NULL; 39 | } 40 | if (curr) 41 | { 42 | free(curr); 43 | curr = NULL; 44 | } 45 | g_select.args = NULL; 46 | } 47 | 48 | /* 49 | ** Removes the active argument from the list and deletes the real file from 50 | ** the system too if 'real' mode is on and if the argument is a valid file 51 | ** 52 | ** It stops the program if there's no argument left. 53 | ** 54 | ** @param N/A 55 | ** @return N/A 56 | */ 57 | 58 | void delete_active_arg(void) 59 | { 60 | t_arg *active; 61 | 62 | if (!g_select.active_arg) 63 | return ; 64 | active = *g_select.active_arg; 65 | if (g_select.args == active) 66 | g_select.args = (active->next == active) ? NULL : active->next; 67 | else 68 | g_select.active_arg = &active->next; 69 | active->next->prev = active->prev; 70 | active->prev->next = active->next; 71 | if (g_select.real_mode) 72 | system_delete_arg(active->value); 73 | g_select.argc--; 74 | free(active->value); 75 | active->value = NULL; 76 | free(active); 77 | active = NULL; 78 | if (!g_select.argc) 79 | stop_signal_handler(); 80 | } 81 | 82 | /* 83 | ** Returns the type of the passed in value 84 | ** 85 | ** @param value The value to check the type of 86 | ** @return The appropriate type of the value 87 | */ 88 | 89 | static t_type get_arg_type(char *value) 90 | { 91 | char *name; 92 | 93 | name = ft_strrchr(value, '/') ? ft_strrchr(value, '/') + 1 : value; 94 | if (ft_strendswith(name, ".c")) 95 | return (C_T); 96 | if (ft_strendswith(name, ".o")) 97 | return (O_T); 98 | if (ft_strendswith(name, ".h")) 99 | return (H_T); 100 | if (ft_strendswith(name, ".a")) 101 | return (A_T); 102 | if (ft_strequ(name, "Makefile")) 103 | return (MAKEFILE_T); 104 | if (name[0] == '.') 105 | return (DOT_T); 106 | return (UNKNOWN_T); 107 | } 108 | 109 | /* 110 | ** Appends a value as an argument into the list of arguments or creates it 111 | ** if it doesn't exist 112 | ** 113 | ** @param value The value to insert 114 | ** @return N/A 115 | */ 116 | 117 | void insert_arg(char *value) 118 | { 119 | t_arg *new; 120 | t_arg *last; 121 | 122 | new = (t_arg *)ft_memalloc(sizeof(t_arg)); 123 | new->value = ft_strdup(value); 124 | new->type = get_arg_type(value); 125 | g_select.argc++; 126 | if (!g_select.args) 127 | { 128 | new->prev = new; 129 | new->next = new; 130 | g_select.args = new; 131 | g_select.active_arg = &g_select.args; 132 | return ; 133 | } 134 | last = g_select.args->prev; 135 | new->next = g_select.args; 136 | g_select.args->prev = new; 137 | new->prev = last; 138 | last->next = new; 139 | } 140 | 141 | /* 142 | ** Initializes the list of argument values so we can display them later 143 | ** 144 | ** @param av Arguments variable 145 | ** @return N/A 146 | */ 147 | 148 | void init_args(char **av) 149 | { 150 | int i; 151 | 152 | i = -1; 153 | g_select.argc = 0; 154 | while (av[++i]) 155 | insert_arg(av[i]); 156 | g_select.selected_count = 0; 157 | } 158 | -------------------------------------------------------------------------------- /src/main.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* main.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/06/27 20:47:46 by jrameau #+# #+# */ 9 | /* Updated: 2017/07/04 08:02:38 by nick ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include 14 | 15 | /* 16 | ** Loads the entry for the passed in terminal name 17 | ** 18 | ** Makes sure the file descriptor is a valid terminal type device, 19 | ** then loads the entry from the terminfo database for name, if it exists. 20 | ** I'm using STDERR_FILENO instead of STDOUT_FILENO to take care of command 21 | ** substitutions so that we don't use the same file descriptor as the calling 22 | ** command. Using STDIN_FILENO would have been a problem too for some commands 23 | ** i.e.: rm `./ft_ls test1 test2` 24 | ** 25 | ** @param tty_name entry name received from the environment variable 26 | ** @return N/A 27 | */ 28 | 29 | static void load_entry(char *tty_name) 30 | { 31 | int res; 32 | char buf[1024]; 33 | 34 | if (!isatty(STDERR_FILENO)) 35 | { 36 | ft_putendl_fd("Not a terminal.", STDERR_FILENO); 37 | exit(EXIT_FAILURE); 38 | } 39 | if ((res = tgetent(buf, tty_name)) < 1) 40 | { 41 | if (res == -1) 42 | ft_putendl_fd("Terminfo database not found. Exiting.", 43 | STDERR_FILENO); 44 | else if (res == 0) 45 | ft_putendl_fd("No such entry in the terminfo database. Exiting.", 46 | STDERR_FILENO); 47 | exit(EXIT_FAILURE); 48 | } 49 | } 50 | 51 | /* 52 | ** Prints all selected arguments 53 | ** 54 | ** @param void 55 | ** @return N/A 56 | */ 57 | 58 | static void print_selected_args(void) 59 | { 60 | t_arg *args; 61 | t_arg *first; 62 | int count; 63 | 64 | args = g_select.args; 65 | first = args; 66 | count = 1; 67 | while (args) 68 | { 69 | if (args->is_selected) 70 | { 71 | print_value_fd(args, STDOUT_FILENO); 72 | count++; 73 | if (count < g_select.selected_count) 74 | ft_putstr_fd(" ", STDOUT_FILENO); 75 | } 76 | if (args->next == first) 77 | break ; 78 | args = args->next; 79 | } 80 | } 81 | 82 | /* 83 | ** Initializes the custom terminal configurations 84 | ** 85 | ** It first checks that we can access the terminal name from the environment, 86 | ** then loads the entry for the terminal name we just got, 87 | ** saves the old attributes to reset them later 88 | ** and applies our custom attributes to the terminal. 89 | ** The ids "ti" and "vi" enter alternate screen mode (so we can later give back 90 | ** the shell the way it was) and hide the cursor respectively 91 | ** 92 | ** @param N/A 93 | ** @return N/A 94 | */ 95 | 96 | void init_custom_conf(void) 97 | { 98 | if (!(g_select.term_name = getenv("TERM"))) 99 | { 100 | ft_putendl_fd("Could not find the terminal name.", STDERR_FILENO); 101 | exit(EXIT_SUCCESS); 102 | } 103 | load_entry(g_select.term_name); 104 | tcgetattr(STDERR_FILENO, &g_select.old_attr); 105 | tcgetattr(STDERR_FILENO, &g_select.attr); 106 | g_select.attr.c_lflag &= ~(ICANON | ECHO); 107 | g_select.attr.c_cc[VMIN] = 1; 108 | g_select.attr.c_cc[VTIME] = 0; 109 | tcsetattr(STDERR_FILENO, TCSANOW, &g_select.attr); 110 | tputs(tgetstr("ti", NULL), 1, ft_printnbr); 111 | tputs(tgetstr("vi", NULL), 1, ft_printnbr); 112 | } 113 | 114 | /* 115 | ** Initializes the signal handlers 116 | ** 117 | ** @param N/A 118 | ** @return N/A 119 | */ 120 | 121 | void init_signal_handlers(void) 122 | { 123 | signal(SIGWINCH, signal_handler); 124 | signal(SIGABRT, signal_handler); 125 | signal(SIGINT, signal_handler); 126 | signal(SIGSTOP, signal_handler); 127 | signal(SIGCONT, signal_handler); 128 | signal(SIGTSTP, signal_handler); 129 | signal(SIGKILL, signal_handler); 130 | signal(SIGQUIT, signal_handler); 131 | } 132 | 133 | /* 134 | ** Initializes ft_select 135 | ** 136 | ** @param ac Arguments count 137 | ** @param av Arguments variable 138 | ** @return 0 on completion 139 | */ 140 | 141 | int main(int ac, char **av) 142 | { 143 | if (ac < 2 || (ac == 2 && av[1][0] == '-')) 144 | print_usage(); 145 | if (av[1][0] == '-' && av[1][1]) 146 | validate_flag(av[1] + 1); 147 | init_custom_conf(); 148 | init_args((av[1][0] == '-') ? av + 2 : av + 1); 149 | init_signal_handlers(); 150 | on_key_press(); 151 | reset_default_conf(); 152 | print_selected_args(); 153 | free_args(); 154 | return (0); 155 | } 156 | -------------------------------------------------------------------------------- /src/print_usage.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* print_usage.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/06/27 20:52:07 by jrameau #+# #+# */ 9 | /* Updated: 2017/07/03 02:47:23 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "ft_select.h" 14 | 15 | /* 16 | ** Print usage instructions 17 | ** 18 | ** @param N/A 19 | ** @return N/A 20 | */ 21 | 22 | void print_usage(void) 23 | { 24 | ft_putendl_fd("Thank you for using ft_select. Here's how to use it:", 25 | STDERR_FILENO); 26 | ft_putendl_fd("./ft_select [-r | --real] argument1 [argument2...]", 27 | STDERR_FILENO); 28 | exit(EXIT_FAILURE); 29 | } 30 | -------------------------------------------------------------------------------- /src/signal_handler.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* signal_handler.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/06/28 22:38:53 by jrameau #+# #+# */ 9 | /* Updated: 2017/07/04 08:02:57 by nick ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include 14 | 15 | /* 16 | ** Handles the suspend signal (ctrl + z) 17 | ** 18 | ** It resets the default terminal configurations, 19 | ** sets the suspend signal back to the default behavior 20 | ** and yields the suspend signal (/032 or /x1A) to keep suspending the program. 21 | ** 22 | ** @param N/A 23 | ** @return N/A 24 | */ 25 | 26 | static void suspend_signal_handler(void) 27 | { 28 | reset_default_conf(); 29 | signal(SIGTSTP, SIG_DFL); 30 | ioctl(STDERR_FILENO, TIOCSTI, "\x1A"); 31 | } 32 | 33 | /* 34 | ** Handles most of the stop signals (ctrl + c, kill, etc...) 35 | ** 36 | ** It resets the default terminal configurations, 37 | ** frees all allocated memories from the global variable 38 | ** and exits 39 | ** 40 | ** @param N/A 41 | ** @return N/A 42 | */ 43 | 44 | void stop_signal_handler(void) 45 | { 46 | reset_default_conf(); 47 | free_args(); 48 | exit(EXIT_SUCCESS); 49 | } 50 | 51 | /* 52 | ** Handles all the signals initilized during 'init_signal_handlers' 53 | ** 54 | ** @param signo The signal number of the current signal 55 | ** @return N/A 56 | */ 57 | 58 | void signal_handler(int signo) 59 | { 60 | if (signo == SIGTSTP) 61 | suspend_signal_handler(); 62 | else if (signo == SIGINT || signo == SIGABRT || signo == SIGSTOP 63 | || signo == SIGKILL || signo == SIGQUIT) 64 | stop_signal_handler(); 65 | else if (signo == SIGCONT) 66 | { 67 | init_custom_conf(); 68 | init_signal_handlers(); 69 | column_display(); 70 | } 71 | else if (signo == SIGWINCH) 72 | column_display(); 73 | } 74 | -------------------------------------------------------------------------------- /src/utils.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* utils.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/07/02 17:41:08 by jrameau #+# #+# */ 9 | /* Updated: 2017/07/04 07:17:06 by nick ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include 14 | 15 | /* 16 | ** Returns the appropriate direction depending on the value of 'c' 17 | ** 18 | ** @param c The pressed key, represented as a long integer 19 | ** for easier detection 20 | ** @return The direction that matches the pressed key 21 | */ 22 | 23 | static t_dir get_dir(long c) 24 | { 25 | if (c == UP_KEY) 26 | return (UP_DIR); 27 | else if (c == DOWN_KEY) 28 | return (DOWN_DIR); 29 | else if (c == RIGHT_KEY) 30 | return (RIGHT_DIR); 31 | else if (c == LEFT_KEY) 32 | return (LEFT_DIR); 33 | else 34 | return (DEFAULT_DIR); 35 | } 36 | 37 | /* 38 | ** Toggles the selection state of the active argument 39 | ** 40 | ** On selection, sets the next argument as active 41 | ** 42 | ** @param N/A 43 | ** @return N/A 44 | */ 45 | 46 | static void toggle_selection(void) 47 | { 48 | (*g_select.active_arg)->is_selected = !(*g_select.active_arg)->is_selected; 49 | g_select.selected_count += ((*g_select.active_arg)->is_selected) ? 1 : -1; 50 | if ((*g_select.active_arg)->is_selected) 51 | g_select.active_arg = &(*g_select.active_arg)->next; 52 | } 53 | 54 | /* 55 | ** Validates the passed in flag 'arg' 56 | ** 57 | ** @param arg The passed flag without the first '-' 58 | ** @return N/A 59 | */ 60 | 61 | void validate_flag(char *arg) 62 | { 63 | if ((arg[0] == 'r' && !arg[1]) || (arg[0] == '-' && 64 | ft_strequ(arg + 1, "real"))) 65 | g_select.real_mode = 1; 66 | else 67 | print_usage(); 68 | } 69 | 70 | /* 71 | ** Provides folder browsing capabilities 72 | ** 73 | ** Press 'O' on a folder to open and show its files and press 'B' to go 74 | ** back to the parent root. 75 | ** 76 | ** @param key The pressed key (O_KEY or B_KEY) 77 | ** @return N/A 78 | */ 79 | 80 | static void folder_browsing(int key) 81 | { 82 | DIR *dir; 83 | struct dirent *entry; 84 | char *name; 85 | char *cwd; 86 | 87 | cwd = getcwd(NULL, MAXPATHLEN); 88 | name = (key == O_KEY) 89 | ? ft_pathjoin(cwd, (*g_select.active_arg)->value) 90 | : ft_get_parent_path(cwd); 91 | free(cwd); 92 | if (!(dir = opendir(name))) 93 | return (free(name)); 94 | free_args(); 95 | while ((entry = readdir(dir))) 96 | { 97 | if (entry->d_name[0] == '.') 98 | continue ; 99 | insert_arg(entry->d_name); 100 | } 101 | closedir(dir); 102 | chdir(name); 103 | free(name); 104 | name = NULL; 105 | } 106 | 107 | /* 108 | ** Displays, in a loop, the columns of values and handles key presses 109 | ** 110 | ** Handles the following keys 111 | ** - Enter key: End the program 112 | ** - Space key: Select or unselect the active argument 113 | ** - Escape key: Stop the program 114 | ** - Backspace/Delete keys: Delete the active argument 115 | ** - Star key: Selects every argument 116 | ** - Minus key: Unselect every argument 117 | ** - O key: Opens and display the active argument if it's a valid folder 118 | ** - B key: Opens the parent of the current folder 119 | ** - Top/Right/Bottom/Left key: navigation directions in the browser 120 | ** 121 | ** @param N/A 122 | ** @return N/A 123 | */ 124 | 125 | void on_key_press(void) 126 | { 127 | long c; 128 | 129 | while (1) 130 | { 131 | column_display(); 132 | c = 0; 133 | read(STDERR_FILENO, &c, 8); 134 | if (c == ENTER_KEY) 135 | break ; 136 | else if (c == SPC_KEY) 137 | toggle_selection(); 138 | else if (c == ESC_KEY) 139 | stop_signal_handler(); 140 | else if (c == BSP_KEY || c == DEL_KEY) 141 | delete_active_arg(); 142 | else if (c == STAR_KEY || c == MINUS_KEY) 143 | toggle_all_args(c); 144 | else if (c == O_KEY || c == B_KEY) 145 | folder_browsing(c); 146 | else 147 | move(get_dir(c)); 148 | } 149 | } 150 | -------------------------------------------------------------------------------- /src/utils2.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* utils2.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/07/02 17:48:46 by jrameau #+# #+# */ 9 | /* Updated: 2017/07/03 02:47:18 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include 14 | 15 | /* 16 | ** Resets the old attributes that we saved from 'init_custom_conf' 17 | ** 18 | ** It applies the old atributes to the terminal 19 | ** The ids "ve" amd "te" exit alternate screen mode and show the cursor 20 | ** respectively. 21 | ** 22 | ** @param N/A 23 | ** @return N/A 24 | */ 25 | 26 | void reset_default_conf(void) 27 | { 28 | tcsetattr(STDERR_FILENO, TCSANOW, &g_select.old_attr); 29 | tputs(tgetstr("ve", NULL), 1, ft_printnbr); 30 | tputs(tgetstr("te", NULL), 1, ft_printnbr); 31 | } 32 | 33 | /* 34 | ** Selects or unselects all arguments from the list depending on 'key' 35 | ** 36 | ** @param key The pressed key (STAR_KEY or MINUS_KEY) 37 | ** @return N/A 38 | */ 39 | 40 | void toggle_all_args(long key) 41 | { 42 | t_arg *args; 43 | t_arg *first; 44 | 45 | args = g_select.args; 46 | first = args; 47 | while (args) 48 | { 49 | args->is_selected = (key == STAR_KEY) ? 1 : 0; 50 | g_select.selected_count += (key == STAR_KEY) ? 1 : -1; 51 | if (args->next == first) 52 | break ; 53 | args = args->next; 54 | } 55 | } 56 | 57 | /* 58 | ** Yet another implementation of putchar 59 | ** 60 | ** My libft's ft_putchar doesn't use the same data types, and I would rather 61 | ** not modify it for compatibility issues. 62 | ** 63 | ** @param nbr The char/number to print 64 | ** @return The amount of bytes written 65 | */ 66 | 67 | int ft_printnbr(int nbr) 68 | { 69 | return (write(STDERR_FILENO, &nbr, 1)); 70 | } 71 | 72 | /* 73 | ** Returns the length of the longest value from the list of arguments 74 | ** 75 | ** @param N/A 76 | ** @return The length of the longest value 77 | */ 78 | 79 | int count_max_arg_len(void) 80 | { 81 | int max; 82 | int curr_len; 83 | t_arg *first; 84 | t_arg *tmp; 85 | 86 | if (!g_select.args || !g_select.args->value) 87 | return (0); 88 | max = 0; 89 | tmp = g_select.args; 90 | first = tmp; 91 | while (tmp) 92 | { 93 | curr_len = ft_strlen(tmp->value); 94 | if (curr_len > max) 95 | max = curr_len; 96 | if (tmp->next == first) 97 | break ; 98 | tmp = tmp->next; 99 | } 100 | return (max); 101 | } 102 | -------------------------------------------------------------------------------- /src/utils3.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* utils3.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/07/03 01:31:56 by jrameau #+# #+# */ 9 | /* Updated: 2017/07/03 01:46:08 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include 14 | 15 | /* 16 | ** The callback to pass to nftw 17 | ** 18 | ** @param fpath The path to walk through 19 | ** @param sb pointer to the stat buffer containing information on 20 | ** the current file 21 | ** @param typeflag Additional information on the current file 22 | ** @param ftwbuf A pointer to a FTW structure 23 | */ 24 | 25 | static int delete_callback(const char *fpath, const struct stat *sb, 26 | int typeflag, struct FTW *ftwbuf) 27 | { 28 | int rm; 29 | 30 | (void)sb; 31 | (void)typeflag; 32 | (void)ftwbuf; 33 | rm = remove(fpath); 34 | return (rm); 35 | } 36 | 37 | /* 38 | ** Deletes the passed in 'path' from the system if it's a valid file or folder 39 | ** 40 | ** It uses 'nftw' (file tree walk) to delete folders (mainly for non empty ones) 41 | ** and 'remove' for files. 42 | ** 43 | ** @param path The path to the entry to delete 44 | ** @return N/A 45 | */ 46 | 47 | void system_delete_arg(char *path) 48 | { 49 | struct stat f; 50 | 51 | if (lstat(path, &f) == -1) 52 | return ; 53 | if (S_ISDIR(f.st_mode)) 54 | nftw(path, delete_callback, 64, FTW_DEPTH | FTW_PHYS); 55 | else 56 | remove(path); 57 | } 58 | --------------------------------------------------------------------------------