skeletton untested project
This commit is contained in:
commit
6fc620e8f4
187 changed files with 6584 additions and 0 deletions
31
includes/ft_strace.h
Normal file
31
includes/ft_strace.h
Normal file
|
|
@ -0,0 +1,31 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_strace.h :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2025/12/11 04:06:00 by thrieg #+# #+# */
|
||||||
|
/* Updated: 2025/12/11 05:54:47 by thrieg ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#ifndef FT_STRACE_H
|
||||||
|
#define FT_STRACE_H
|
||||||
|
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <sys/ptrace.h>
|
||||||
|
#include "../libft/libft.h"
|
||||||
|
|
||||||
|
//returns 64 for x84_64, or 32 for 32 bits, -1 for open/read error, -2 for unrecognised file type
|
||||||
|
ssize_t binary_type(char *path_to_binary);
|
||||||
|
|
||||||
|
typedef struct s_syscall_desc {
|
||||||
|
const char *name;
|
||||||
|
unsigned char argc;
|
||||||
|
} t_syscall_desc;
|
||||||
|
|
||||||
|
#endif
|
||||||
353
includes/syscalls_x64.h
Normal file
353
includes/syscalls_x64.h
Normal file
|
|
@ -0,0 +1,353 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* syscalls_x64.h :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2025/12/11 05:55:53 by thrieg #+# #+# */
|
||||||
|
/* Updated: 2025/12/11 05:58:18 by thrieg ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "ft_strace.h"
|
||||||
|
|
||||||
|
/* Auto-generated from https://x64.syscall.sh/ */
|
||||||
|
const t_syscall_desc g_syscalls_64[] = {
|
||||||
|
[ 0] = { "read", 3 },
|
||||||
|
[ 1] = { "write", 3 },
|
||||||
|
[ 2] = { "open", 3 },
|
||||||
|
[ 3] = { "close", 1 },
|
||||||
|
[ 4] = { "stat", 2 },
|
||||||
|
[ 5] = { "fstat", 2 },
|
||||||
|
[ 6] = { "lstat", 2 },
|
||||||
|
[ 7] = { "poll", 3 },
|
||||||
|
[ 8] = { "lseek", 3 },
|
||||||
|
[ 9] = { "mmap", 6 },
|
||||||
|
[ 10] = { "mprotect", 3 },
|
||||||
|
[ 11] = { "munmap", 2 },
|
||||||
|
[ 12] = { "brk", 1 },
|
||||||
|
[ 13] = { "rt_sigaction", 4 },
|
||||||
|
[ 14] = { "rt_sigprocmask", 4 },
|
||||||
|
[ 15] = { "rt_sigreturn", 6 },
|
||||||
|
[ 16] = { "ioctl", 3 },
|
||||||
|
[ 17] = { "pread64", 4 },
|
||||||
|
[ 18] = { "pwrite64", 4 },
|
||||||
|
[ 19] = { "readv", 3 },
|
||||||
|
[ 20] = { "writev", 3 },
|
||||||
|
[ 21] = { "access", 2 },
|
||||||
|
[ 22] = { "pipe", 1 },
|
||||||
|
[ 23] = { "select", 5 },
|
||||||
|
[ 24] = { "sched_yield", 0 },
|
||||||
|
[ 25] = { "mremap", 5 },
|
||||||
|
[ 26] = { "msync", 3 },
|
||||||
|
[ 27] = { "mincore", 3 },
|
||||||
|
[ 28] = { "madvise", 3 },
|
||||||
|
[ 29] = { "shmget", 3 },
|
||||||
|
[ 30] = { "shmat", 3 },
|
||||||
|
[ 31] = { "shmctl", 3 },
|
||||||
|
[ 32] = { "dup", 1 },
|
||||||
|
[ 33] = { "dup2", 2 },
|
||||||
|
[ 34] = { "pause", 0 },
|
||||||
|
[ 35] = { "nanosleep", 2 },
|
||||||
|
[ 36] = { "getitimer", 2 },
|
||||||
|
[ 37] = { "alarm", 1 },
|
||||||
|
[ 38] = { "setitimer", 3 },
|
||||||
|
[ 39] = { "getpid", 0 },
|
||||||
|
[ 40] = { "sendfile", 4 },
|
||||||
|
[ 41] = { "socket", 3 },
|
||||||
|
[ 42] = { "connect", 3 },
|
||||||
|
[ 43] = { "accept", 3 },
|
||||||
|
[ 44] = { "sendto", 6 },
|
||||||
|
[ 45] = { "recvfrom", 6 },
|
||||||
|
[ 46] = { "sendmsg", 3 },
|
||||||
|
[ 47] = { "recvmsg", 3 },
|
||||||
|
[ 48] = { "shutdown", 2 },
|
||||||
|
[ 49] = { "bind", 3 },
|
||||||
|
[ 50] = { "listen", 2 },
|
||||||
|
[ 51] = { "getsockname", 3 },
|
||||||
|
[ 52] = { "getpeername", 3 },
|
||||||
|
[ 53] = { "socketpair", 4 },
|
||||||
|
[ 54] = { "setsockopt", 5 },
|
||||||
|
[ 55] = { "getsockopt", 5 },
|
||||||
|
[ 56] = { "clone", 5 },
|
||||||
|
[ 57] = { "fork", 0 },
|
||||||
|
[ 58] = { "vfork", 0 },
|
||||||
|
[ 59] = { "execve", 3 },
|
||||||
|
[ 60] = { "exit", 1 },
|
||||||
|
[ 61] = { "wait4", 4 },
|
||||||
|
[ 62] = { "kill", 2 },
|
||||||
|
[ 63] = { "uname", 1 },
|
||||||
|
[ 64] = { "semget", 3 },
|
||||||
|
[ 65] = { "semop", 3 },
|
||||||
|
[ 66] = { "semctl", 4 },
|
||||||
|
[ 67] = { "shmdt", 1 },
|
||||||
|
[ 68] = { "msgget", 2 },
|
||||||
|
[ 69] = { "msgsnd", 4 },
|
||||||
|
[ 70] = { "msgrcv", 5 },
|
||||||
|
[ 71] = { "msgctl", 3 },
|
||||||
|
[ 72] = { "fcntl", 3 },
|
||||||
|
[ 73] = { "flock", 2 },
|
||||||
|
[ 74] = { "fsync", 1 },
|
||||||
|
[ 75] = { "fdatasync", 1 },
|
||||||
|
[ 76] = { "truncate", 2 },
|
||||||
|
[ 77] = { "ftruncate", 2 },
|
||||||
|
[ 78] = { "getdents", 3 },
|
||||||
|
[ 79] = { "getcwd", 2 },
|
||||||
|
[ 80] = { "chdir", 1 },
|
||||||
|
[ 81] = { "fchdir", 1 },
|
||||||
|
[ 82] = { "rename", 2 },
|
||||||
|
[ 83] = { "mkdir", 2 },
|
||||||
|
[ 84] = { "rmdir", 1 },
|
||||||
|
[ 85] = { "creat", 2 },
|
||||||
|
[ 86] = { "link", 2 },
|
||||||
|
[ 87] = { "unlink", 1 },
|
||||||
|
[ 88] = { "symlink", 2 },
|
||||||
|
[ 89] = { "readlink", 3 },
|
||||||
|
[ 90] = { "chmod", 2 },
|
||||||
|
[ 91] = { "fchmod", 2 },
|
||||||
|
[ 92] = { "chown", 3 },
|
||||||
|
[ 93] = { "fchown", 3 },
|
||||||
|
[ 94] = { "lchown", 3 },
|
||||||
|
[ 95] = { "umask", 1 },
|
||||||
|
[ 96] = { "gettimeofday", 2 },
|
||||||
|
[ 97] = { "getrlimit", 2 },
|
||||||
|
[ 98] = { "getrusage", 2 },
|
||||||
|
[ 99] = { "sysinfo", 1 },
|
||||||
|
[100] = { "times", 1 },
|
||||||
|
[101] = { "ptrace", 4 },
|
||||||
|
[102] = { "getuid", 0 },
|
||||||
|
[103] = { "syslog", 3 },
|
||||||
|
[104] = { "getgid", 0 },
|
||||||
|
[105] = { "setuid", 1 },
|
||||||
|
[106] = { "setgid", 1 },
|
||||||
|
[107] = { "geteuid", 0 },
|
||||||
|
[108] = { "getegid", 0 },
|
||||||
|
[109] = { "setpgid", 2 },
|
||||||
|
[110] = { "getppid", 0 },
|
||||||
|
[111] = { "getpgrp", 0 },
|
||||||
|
[112] = { "setsid", 0 },
|
||||||
|
[113] = { "setreuid", 2 },
|
||||||
|
[114] = { "setregid", 2 },
|
||||||
|
[115] = { "getgroups", 2 },
|
||||||
|
[116] = { "setgroups", 2 },
|
||||||
|
[117] = { "setresuid", 3 },
|
||||||
|
[118] = { "getresuid", 3 },
|
||||||
|
[119] = { "setresgid", 3 },
|
||||||
|
[120] = { "getresgid", 3 },
|
||||||
|
[121] = { "getpgid", 1 },
|
||||||
|
[122] = { "setfsuid", 1 },
|
||||||
|
[123] = { "setfsgid", 1 },
|
||||||
|
[124] = { "getsid", 1 },
|
||||||
|
[125] = { "capget", 2 },
|
||||||
|
[126] = { "capset", 2 },
|
||||||
|
[127] = { "rt_sigpending", 2 },
|
||||||
|
[128] = { "rt_sigtimedwait", 4 },
|
||||||
|
[129] = { "rt_sigqueueinfo", 3 },
|
||||||
|
[130] = { "rt_sigsuspend", 2 },
|
||||||
|
[131] = { "sigaltstack", 2 },
|
||||||
|
[132] = { "utime", 2 },
|
||||||
|
[133] = { "mknod", 3 },
|
||||||
|
[134] = { "uselib", 1 },
|
||||||
|
[135] = { "personality", 1 },
|
||||||
|
[136] = { "ustat", 2 },
|
||||||
|
[137] = { "statfs", 2 },
|
||||||
|
[138] = { "fstatfs", 2 },
|
||||||
|
[139] = { "sysfs", 3 },
|
||||||
|
[140] = { "getpriority", 2 },
|
||||||
|
[141] = { "setpriority", 3 },
|
||||||
|
[142] = { "sched_setparam", 2 },
|
||||||
|
[143] = { "sched_getparam", 2 },
|
||||||
|
[144] = { "sched_setscheduler", 3 },
|
||||||
|
[145] = { "sched_getscheduler", 1 },
|
||||||
|
[146] = { "sched_get_priority_max", 1 },
|
||||||
|
[147] = { "sched_get_priority_min", 1 },
|
||||||
|
[148] = { "sched_rr_get_interval", 2 },
|
||||||
|
[149] = { "mlock", 2 },
|
||||||
|
[150] = { "munlock", 2 },
|
||||||
|
[151] = { "mlockall", 1 },
|
||||||
|
[152] = { "munlockall", 0 },
|
||||||
|
[153] = { "vhangup", 0 },
|
||||||
|
[154] = { "modify_ldt", 6 },
|
||||||
|
[155] = { "pivot_root", 2 },
|
||||||
|
[156] = { "_sysctl", 6 },
|
||||||
|
[157] = { "prctl", 5 },
|
||||||
|
[158] = { "arch_prctl", 6 },
|
||||||
|
[159] = { "adjtimex", 1 },
|
||||||
|
[160] = { "setrlimit", 2 },
|
||||||
|
[161] = { "chroot", 1 },
|
||||||
|
[162] = { "sync", 0 },
|
||||||
|
[163] = { "acct", 1 },
|
||||||
|
[164] = { "settimeofday", 2 },
|
||||||
|
[165] = { "mount", 5 },
|
||||||
|
[166] = { "umount2", 6 },
|
||||||
|
[167] = { "swapon", 2 },
|
||||||
|
[168] = { "swapoff", 1 },
|
||||||
|
[169] = { "reboot", 4 },
|
||||||
|
[170] = { "sethostname", 2 },
|
||||||
|
[171] = { "setdomainname", 2 },
|
||||||
|
[172] = { "iopl", 6 },
|
||||||
|
[173] = { "ioperm", 3 },
|
||||||
|
[174] = { "create_module", 6 },
|
||||||
|
[175] = { "init_module", 3 },
|
||||||
|
[176] = { "delete_module", 2 },
|
||||||
|
[177] = { "get_kernel_syms", 6 },
|
||||||
|
[178] = { "query_module", 6 },
|
||||||
|
[179] = { "quotactl", 4 },
|
||||||
|
[180] = { "nfsservctl", 6 },
|
||||||
|
[181] = { "getpmsg", 6 },
|
||||||
|
[182] = { "putpmsg", 6 },
|
||||||
|
[183] = { "afs_syscall", 6 },
|
||||||
|
[184] = { "tuxcall", 6 },
|
||||||
|
[185] = { "security", 6 },
|
||||||
|
[186] = { "gettid", 0 },
|
||||||
|
[187] = { "readahead", 3 },
|
||||||
|
[188] = { "setxattr", 5 },
|
||||||
|
[189] = { "lsetxattr", 5 },
|
||||||
|
[190] = { "fsetxattr", 5 },
|
||||||
|
[191] = { "getxattr", 4 },
|
||||||
|
[192] = { "lgetxattr", 4 },
|
||||||
|
[193] = { "fgetxattr", 4 },
|
||||||
|
[194] = { "listxattr", 3 },
|
||||||
|
[195] = { "llistxattr", 3 },
|
||||||
|
[196] = { "flistxattr", 3 },
|
||||||
|
[197] = { "removexattr", 2 },
|
||||||
|
[198] = { "lremovexattr", 2 },
|
||||||
|
[199] = { "fremovexattr", 2 },
|
||||||
|
[200] = { "tkill", 2 },
|
||||||
|
[201] = { "time", 1 },
|
||||||
|
[202] = { "futex", 6 },
|
||||||
|
[203] = { "sched_setaffinity", 3 },
|
||||||
|
[204] = { "sched_getaffinity", 3 },
|
||||||
|
[205] = { "set_thread_area", 6 },
|
||||||
|
[206] = { "io_setup", 2 },
|
||||||
|
[207] = { "io_destroy", 1 },
|
||||||
|
[208] = { "io_getevents", 5 },
|
||||||
|
[209] = { "io_submit", 3 },
|
||||||
|
[210] = { "io_cancel", 3 },
|
||||||
|
[211] = { "get_thread_area", 6 },
|
||||||
|
[212] = { "lookup_dcookie", 3 },
|
||||||
|
[213] = { "epoll_create", 1 },
|
||||||
|
[214] = { "epoll_ctl_old", 6 },
|
||||||
|
[215] = { "epoll_wait_old", 6 },
|
||||||
|
[216] = { "remap_file_pages", 5 },
|
||||||
|
[217] = { "getdents64", 3 },
|
||||||
|
[218] = { "set_tid_address", 1 },
|
||||||
|
[219] = { "restart_syscall", 0 },
|
||||||
|
[220] = { "semtimedop", 4 },
|
||||||
|
[221] = { "fadvise64", 4 },
|
||||||
|
[222] = { "timer_create", 3 },
|
||||||
|
[223] = { "timer_settime", 4 },
|
||||||
|
[224] = { "timer_gettime", 2 },
|
||||||
|
[225] = { "timer_getoverrun", 1 },
|
||||||
|
[226] = { "timer_delete", 1 },
|
||||||
|
[227] = { "clock_settime", 2 },
|
||||||
|
[228] = { "clock_gettime", 2 },
|
||||||
|
[229] = { "clock_getres", 2 },
|
||||||
|
[230] = { "clock_nanosleep", 4 },
|
||||||
|
[231] = { "exit_group", 1 },
|
||||||
|
[232] = { "epoll_wait", 4 },
|
||||||
|
[233] = { "epoll_ctl", 4 },
|
||||||
|
[234] = { "tgkill", 3 },
|
||||||
|
[235] = { "utimes", 2 },
|
||||||
|
[236] = { "vserver", 6 },
|
||||||
|
[237] = { "mbind", 6 },
|
||||||
|
[238] = { "set_mempolicy", 3 },
|
||||||
|
[239] = { "get_mempolicy", 5 },
|
||||||
|
[240] = { "mq_open", 4 },
|
||||||
|
[241] = { "mq_unlink", 1 },
|
||||||
|
[242] = { "mq_timedsend", 5 },
|
||||||
|
[243] = { "mq_timedreceive", 5 },
|
||||||
|
[244] = { "mq_notify", 2 },
|
||||||
|
[245] = { "mq_getsetattr", 3 },
|
||||||
|
[246] = { "kexec_load", 4 },
|
||||||
|
[247] = { "waitid", 5 },
|
||||||
|
[248] = { "add_key", 5 },
|
||||||
|
[249] = { "request_key", 4 },
|
||||||
|
[250] = { "keyctl", 5 },
|
||||||
|
[251] = { "ioprio_set", 3 },
|
||||||
|
[252] = { "ioprio_get", 2 },
|
||||||
|
[253] = { "inotify_init", 0 },
|
||||||
|
[254] = { "inotify_add_watch", 3 },
|
||||||
|
[255] = { "inotify_rm_watch", 2 },
|
||||||
|
[256] = { "migrate_pages", 4 },
|
||||||
|
[257] = { "openat", 4 },
|
||||||
|
[258] = { "mkdirat", 3 },
|
||||||
|
[259] = { "mknodat", 4 },
|
||||||
|
[260] = { "fchownat", 5 },
|
||||||
|
[261] = { "futimesat", 3 },
|
||||||
|
[262] = { "newfstatat", 4 },
|
||||||
|
[263] = { "unlinkat", 3 },
|
||||||
|
[264] = { "renameat", 4 },
|
||||||
|
[265] = { "linkat", 5 },
|
||||||
|
[266] = { "symlinkat", 3 },
|
||||||
|
[267] = { "readlinkat", 4 },
|
||||||
|
[268] = { "fchmodat", 3 },
|
||||||
|
[269] = { "faccessat", 3 },
|
||||||
|
[270] = { "pselect6", 6 },
|
||||||
|
[271] = { "ppoll", 5 },
|
||||||
|
[272] = { "unshare", 1 },
|
||||||
|
[273] = { "set_robust_list", 2 },
|
||||||
|
[274] = { "get_robust_list", 3 },
|
||||||
|
[275] = { "splice", 6 },
|
||||||
|
[276] = { "tee", 4 },
|
||||||
|
[277] = { "sync_file_range", 4 },
|
||||||
|
[278] = { "vmsplice", 4 },
|
||||||
|
[279] = { "move_pages", 6 },
|
||||||
|
[280] = { "utimensat", 4 },
|
||||||
|
[281] = { "epoll_pwait", 6 },
|
||||||
|
[282] = { "signalfd", 3 },
|
||||||
|
[283] = { "timerfd_create", 2 },
|
||||||
|
[284] = { "eventfd", 1 },
|
||||||
|
[285] = { "fallocate", 4 },
|
||||||
|
[286] = { "timerfd_settime", 4 },
|
||||||
|
[287] = { "timerfd_gettime", 2 },
|
||||||
|
[288] = { "accept4", 4 },
|
||||||
|
[289] = { "signalfd4", 4 },
|
||||||
|
[290] = { "eventfd2", 2 },
|
||||||
|
[291] = { "epoll_create1", 1 },
|
||||||
|
[292] = { "dup3", 3 },
|
||||||
|
[293] = { "pipe2", 2 },
|
||||||
|
[294] = { "inotify_init1", 1 },
|
||||||
|
[295] = { "preadv", 5 },
|
||||||
|
[296] = { "pwritev", 5 },
|
||||||
|
[297] = { "rt_tgsigqueueinfo", 4 },
|
||||||
|
[298] = { "perf_event_open", 5 },
|
||||||
|
[299] = { "recvmmsg", 5 },
|
||||||
|
[300] = { "fanotify_init", 2 },
|
||||||
|
[301] = { "fanotify_mark", 5 },
|
||||||
|
[302] = { "prlimit64", 4 },
|
||||||
|
[303] = { "name_to_handle_at", 5 },
|
||||||
|
[304] = { "open_by_handle_at", 3 },
|
||||||
|
[305] = { "clock_adjtime", 2 },
|
||||||
|
[306] = { "syncfs", 1 },
|
||||||
|
[307] = { "sendmmsg", 4 },
|
||||||
|
[308] = { "setns", 2 },
|
||||||
|
[309] = { "getcpu", 3 },
|
||||||
|
[310] = { "process_vm_readv", 6 },
|
||||||
|
[311] = { "process_vm_writev", 6 },
|
||||||
|
[312] = { "kcmp", 5 },
|
||||||
|
[313] = { "finit_module", 3 },
|
||||||
|
[314] = { "sched_setattr", 3 },
|
||||||
|
[315] = { "sched_getattr", 4 },
|
||||||
|
[316] = { "renameat2", 5 },
|
||||||
|
[317] = { "seccomp", 3 },
|
||||||
|
[318] = { "getrandom", 3 },
|
||||||
|
[319] = { "memfd_create", 2 },
|
||||||
|
[320] = { "kexec_file_load", 5 },
|
||||||
|
[321] = { "bpf", 3 },
|
||||||
|
[322] = { "execveat", 5 },
|
||||||
|
[323] = { "userfaultfd", 1 },
|
||||||
|
[324] = { "membarrier", 2 },
|
||||||
|
[325] = { "mlock2", 3 },
|
||||||
|
[326] = { "copy_file_range", 6 },
|
||||||
|
[327] = { "preadv2", 6 },
|
||||||
|
[328] = { "pwritev2", 6 },
|
||||||
|
[329] = { "pkey_mprotect", 4 },
|
||||||
|
[330] = { "pkey_alloc", 2 },
|
||||||
|
[331] = { "pkey_free", 1 },
|
||||||
|
[332] = { "statx", 5 },
|
||||||
|
};
|
||||||
|
|
||||||
|
const size_t g_syscalls_64_len =
|
||||||
|
sizeof(g_syscalls_64) / sizeof(g_syscalls_64[0]);
|
||||||
405
includes/syscalls_x86.h
Normal file
405
includes/syscalls_x86.h
Normal file
|
|
@ -0,0 +1,405 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* syscalls_x86.h :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2025/12/11 05:56:09 by thrieg #+# #+# */
|
||||||
|
/* Updated: 2025/12/11 05:58:14 by thrieg ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "ft_strace.h"
|
||||||
|
|
||||||
|
/* Auto-generated from https://x86.syscall.sh/ */
|
||||||
|
static const t_syscall_desc g_syscalls_32[] = {
|
||||||
|
[ 0] = { "restart_syscall", 0 },
|
||||||
|
[ 1] = { "exit", 1 },
|
||||||
|
[ 2] = { "fork", 0 },
|
||||||
|
[ 3] = { "read", 3 },
|
||||||
|
[ 4] = { "write", 3 },
|
||||||
|
[ 5] = { "open", 3 },
|
||||||
|
[ 6] = { "close", 1 },
|
||||||
|
[ 7] = { "waitpid", 3 },
|
||||||
|
[ 8] = { "creat", 2 },
|
||||||
|
[ 9] = { "link", 2 },
|
||||||
|
[ 10] = { "unlink", 1 },
|
||||||
|
[ 11] = { "execve", 3 },
|
||||||
|
[ 12] = { "chdir", 1 },
|
||||||
|
[ 13] = { "time", 1 },
|
||||||
|
[ 14] = { "mknod", 3 },
|
||||||
|
[ 15] = { "chmod", 2 },
|
||||||
|
[ 16] = { "lchown", 3 },
|
||||||
|
[ 17] = { "break", 6 },
|
||||||
|
[ 18] = { "oldstat", 6 },
|
||||||
|
[ 19] = { "lseek", 3 },
|
||||||
|
[ 20] = { "getpid", 0 },
|
||||||
|
[ 21] = { "mount", 5 },
|
||||||
|
[ 22] = { "umount", 2 },
|
||||||
|
[ 23] = { "setuid", 1 },
|
||||||
|
[ 24] = { "getuid", 0 },
|
||||||
|
[ 25] = { "stime", 1 },
|
||||||
|
[ 26] = { "ptrace", 4 },
|
||||||
|
[ 27] = { "alarm", 1 },
|
||||||
|
[ 28] = { "oldfstat", 6 },
|
||||||
|
[ 29] = { "pause", 0 },
|
||||||
|
[ 30] = { "utime", 2 },
|
||||||
|
[ 31] = { "stty", 6 },
|
||||||
|
[ 32] = { "gtty", 6 },
|
||||||
|
[ 33] = { "access", 2 },
|
||||||
|
[ 34] = { "nice", 1 },
|
||||||
|
[ 35] = { "ftime", 6 },
|
||||||
|
[ 36] = { "sync", 0 },
|
||||||
|
[ 37] = { "kill", 2 },
|
||||||
|
[ 38] = { "rename", 2 },
|
||||||
|
[ 39] = { "mkdir", 2 },
|
||||||
|
[ 40] = { "rmdir", 1 },
|
||||||
|
[ 41] = { "dup", 1 },
|
||||||
|
[ 42] = { "pipe", 1 },
|
||||||
|
[ 43] = { "times", 1 },
|
||||||
|
[ 44] = { "prof", 6 },
|
||||||
|
[ 45] = { "brk", 1 },
|
||||||
|
[ 46] = { "setgid", 1 },
|
||||||
|
[ 47] = { "getgid", 0 },
|
||||||
|
[ 48] = { "signal", 2 },
|
||||||
|
[ 49] = { "geteuid", 0 },
|
||||||
|
[ 50] = { "getegid", 0 },
|
||||||
|
[ 51] = { "acct", 1 },
|
||||||
|
[ 52] = { "umount2", 6 },
|
||||||
|
[ 53] = { "lock", 6 },
|
||||||
|
[ 54] = { "ioctl", 3 },
|
||||||
|
[ 55] = { "fcntl", 3 },
|
||||||
|
[ 56] = { "mpx", 6 },
|
||||||
|
[ 57] = { "setpgid", 2 },
|
||||||
|
[ 58] = { "ulimit", 6 },
|
||||||
|
[ 59] = { "oldolduname", 6 },
|
||||||
|
[ 60] = { "umask", 1 },
|
||||||
|
[ 61] = { "chroot", 1 },
|
||||||
|
[ 62] = { "ustat", 2 },
|
||||||
|
[ 63] = { "dup2", 2 },
|
||||||
|
[ 64] = { "getppid", 0 },
|
||||||
|
[ 65] = { "getpgrp", 0 },
|
||||||
|
[ 66] = { "setsid", 0 },
|
||||||
|
[ 67] = { "sigaction", 3 },
|
||||||
|
[ 68] = { "sgetmask", 0 },
|
||||||
|
[ 69] = { "ssetmask", 1 },
|
||||||
|
[ 70] = { "setreuid", 2 },
|
||||||
|
[ 71] = { "setregid", 2 },
|
||||||
|
[ 72] = { "sigsuspend", 3 },
|
||||||
|
[ 73] = { "sigpending", 1 },
|
||||||
|
[ 74] = { "sethostname", 2 },
|
||||||
|
[ 75] = { "setrlimit", 2 },
|
||||||
|
[ 76] = { "getrlimit", 2 },
|
||||||
|
[ 77] = { "getrusage", 2 },
|
||||||
|
[ 78] = { "gettimeofday", 2 },
|
||||||
|
[ 79] = { "settimeofday", 2 },
|
||||||
|
[ 80] = { "getgroups", 2 },
|
||||||
|
[ 81] = { "setgroups", 2 },
|
||||||
|
[ 82] = { "select", 5 },
|
||||||
|
[ 83] = { "symlink", 2 },
|
||||||
|
[ 84] = { "oldlstat", 6 },
|
||||||
|
[ 85] = { "readlink", 3 },
|
||||||
|
[ 86] = { "uselib", 1 },
|
||||||
|
[ 87] = { "swapon", 2 },
|
||||||
|
[ 88] = { "reboot", 4 },
|
||||||
|
[ 89] = { "readdir", 6 },
|
||||||
|
[ 90] = { "mmap", 6 },
|
||||||
|
[ 91] = { "munmap", 2 },
|
||||||
|
[ 92] = { "truncate", 2 },
|
||||||
|
[ 93] = { "ftruncate", 2 },
|
||||||
|
[ 94] = { "fchmod", 2 },
|
||||||
|
[ 95] = { "fchown", 3 },
|
||||||
|
[ 96] = { "getpriority", 2 },
|
||||||
|
[ 97] = { "setpriority", 3 },
|
||||||
|
[ 98] = { "profil", 6 },
|
||||||
|
[ 99] = { "statfs", 2 },
|
||||||
|
[100] = { "fstatfs", 2 },
|
||||||
|
[101] = { "ioperm", 3 },
|
||||||
|
[102] = { "socketcall", 2 },
|
||||||
|
[103] = { "syslog", 3 },
|
||||||
|
[104] = { "setitimer", 3 },
|
||||||
|
[105] = { "getitimer", 2 },
|
||||||
|
[106] = { "stat", 2 },
|
||||||
|
[107] = { "lstat", 2 },
|
||||||
|
[108] = { "fstat", 2 },
|
||||||
|
[109] = { "olduname", 1 },
|
||||||
|
[110] = { "iopl", 6 },
|
||||||
|
[111] = { "vhangup", 0 },
|
||||||
|
[112] = { "idle", 6 },
|
||||||
|
[113] = { "vm86old", 6 },
|
||||||
|
[114] = { "wait4", 4 },
|
||||||
|
[115] = { "swapoff", 1 },
|
||||||
|
[116] = { "sysinfo", 1 },
|
||||||
|
[117] = { "ipc", 6 },
|
||||||
|
[118] = { "fsync", 1 },
|
||||||
|
[119] = { "sigreturn", 6 },
|
||||||
|
[120] = { "clone", 5 },
|
||||||
|
[121] = { "setdomainname", 2 },
|
||||||
|
[122] = { "uname", 1 },
|
||||||
|
[123] = { "modify_ldt", 6 },
|
||||||
|
[124] = { "adjtimex", 1 },
|
||||||
|
[125] = { "mprotect", 3 },
|
||||||
|
[126] = { "sigprocmask", 3 },
|
||||||
|
[127] = { "create_module", 6 },
|
||||||
|
[128] = { "init_module", 3 },
|
||||||
|
[129] = { "delete_module", 2 },
|
||||||
|
[130] = { "get_kernel_syms", 6 },
|
||||||
|
[131] = { "quotactl", 4 },
|
||||||
|
[132] = { "getpgid", 1 },
|
||||||
|
[133] = { "fchdir", 1 },
|
||||||
|
[134] = { "bdflush", 2 },
|
||||||
|
[135] = { "sysfs", 3 },
|
||||||
|
[136] = { "personality", 1 },
|
||||||
|
[137] = { "afs_syscall", 6 },
|
||||||
|
[138] = { "setfsuid", 1 },
|
||||||
|
[139] = { "setfsgid", 1 },
|
||||||
|
[140] = { "_llseek", 6 },
|
||||||
|
[141] = { "getdents", 3 },
|
||||||
|
[142] = { "_newselect", 6 },
|
||||||
|
[143] = { "flock", 2 },
|
||||||
|
[144] = { "msync", 3 },
|
||||||
|
[145] = { "readv", 3 },
|
||||||
|
[146] = { "writev", 3 },
|
||||||
|
[147] = { "getsid", 1 },
|
||||||
|
[148] = { "fdatasync", 1 },
|
||||||
|
[149] = { "_sysctl", 6 },
|
||||||
|
[150] = { "mlock", 2 },
|
||||||
|
[151] = { "munlock", 2 },
|
||||||
|
[152] = { "mlockall", 1 },
|
||||||
|
[153] = { "munlockall", 0 },
|
||||||
|
[154] = { "sched_setparam", 2 },
|
||||||
|
[155] = { "sched_getparam", 2 },
|
||||||
|
[156] = { "sched_setscheduler", 3 },
|
||||||
|
[157] = { "sched_getscheduler", 1 },
|
||||||
|
[158] = { "sched_yield", 0 },
|
||||||
|
[159] = { "sched_get_priority_max", 1 },
|
||||||
|
[160] = { "sched_get_priority_min", 1 },
|
||||||
|
[161] = { "sched_rr_get_interval", 2 },
|
||||||
|
[162] = { "nanosleep", 2 },
|
||||||
|
[163] = { "mremap", 5 },
|
||||||
|
[164] = { "setresuid", 3 },
|
||||||
|
[165] = { "getresuid", 3 },
|
||||||
|
[166] = { "vm86", 6 },
|
||||||
|
[167] = { "query_module", 6 },
|
||||||
|
[168] = { "poll", 3 },
|
||||||
|
[169] = { "nfsservctl", 6 },
|
||||||
|
[170] = { "setresgid", 3 },
|
||||||
|
[171] = { "getresgid", 3 },
|
||||||
|
[172] = { "prctl", 5 },
|
||||||
|
[173] = { "rt_sigreturn", 6 },
|
||||||
|
[174] = { "rt_sigaction", 4 },
|
||||||
|
[175] = { "rt_sigprocmask", 4 },
|
||||||
|
[176] = { "rt_sigpending", 2 },
|
||||||
|
[177] = { "rt_sigtimedwait", 4 },
|
||||||
|
[178] = { "rt_sigqueueinfo", 3 },
|
||||||
|
[179] = { "rt_sigsuspend", 2 },
|
||||||
|
[180] = { "pread64", 4 },
|
||||||
|
[181] = { "pwrite64", 4 },
|
||||||
|
[182] = { "chown", 3 },
|
||||||
|
[183] = { "getcwd", 2 },
|
||||||
|
[184] = { "capget", 2 },
|
||||||
|
[185] = { "capset", 2 },
|
||||||
|
[186] = { "sigaltstack", 2 },
|
||||||
|
[187] = { "sendfile", 4 },
|
||||||
|
[188] = { "getpmsg", 6 },
|
||||||
|
[189] = { "putpmsg", 6 },
|
||||||
|
[190] = { "vfork", 0 },
|
||||||
|
[191] = { "ugetrlimit", 6 },
|
||||||
|
[192] = { "mmap2", 6 },
|
||||||
|
[193] = { "truncate64", 2 },
|
||||||
|
[194] = { "ftruncate64", 2 },
|
||||||
|
[195] = { "stat64", 2 },
|
||||||
|
[196] = { "lstat64", 2 },
|
||||||
|
[197] = { "fstat64", 2 },
|
||||||
|
[198] = { "lchown32", 6 },
|
||||||
|
[199] = { "getuid32", 6 },
|
||||||
|
[200] = { "getgid32", 6 },
|
||||||
|
[201] = { "geteuid32", 6 },
|
||||||
|
[202] = { "getegid32", 6 },
|
||||||
|
[203] = { "setreuid32", 6 },
|
||||||
|
[204] = { "setregid32", 6 },
|
||||||
|
[205] = { "getgroups32", 6 },
|
||||||
|
[206] = { "setgroups32", 6 },
|
||||||
|
[207] = { "fchown32", 6 },
|
||||||
|
[208] = { "setresuid32", 6 },
|
||||||
|
[209] = { "getresuid32", 6 },
|
||||||
|
[210] = { "setresgid32", 6 },
|
||||||
|
[211] = { "getresgid32", 6 },
|
||||||
|
[212] = { "chown32", 6 },
|
||||||
|
[213] = { "setuid32", 6 },
|
||||||
|
[214] = { "setgid32", 6 },
|
||||||
|
[215] = { "setfsuid32", 6 },
|
||||||
|
[216] = { "setfsgid32", 6 },
|
||||||
|
[217] = { "pivot_root", 2 },
|
||||||
|
[218] = { "mincore", 3 },
|
||||||
|
[219] = { "madvise", 3 },
|
||||||
|
[220] = { "getdents64", 3 },
|
||||||
|
[221] = { "fcntl64", 3 },
|
||||||
|
[222] = { "not implemented", 0 },
|
||||||
|
[223] = { "not implemented", 0 },
|
||||||
|
[224] = { "gettid", 0 },
|
||||||
|
[225] = { "readahead", 3 },
|
||||||
|
[226] = { "setxattr", 5 },
|
||||||
|
[227] = { "lsetxattr", 5 },
|
||||||
|
[228] = { "fsetxattr", 5 },
|
||||||
|
[229] = { "getxattr", 4 },
|
||||||
|
[230] = { "lgetxattr", 4 },
|
||||||
|
[231] = { "fgetxattr", 4 },
|
||||||
|
[232] = { "listxattr", 3 },
|
||||||
|
[233] = { "llistxattr", 3 },
|
||||||
|
[234] = { "flistxattr", 3 },
|
||||||
|
[235] = { "removexattr", 2 },
|
||||||
|
[236] = { "lremovexattr", 2 },
|
||||||
|
[237] = { "fremovexattr", 2 },
|
||||||
|
[238] = { "tkill", 2 },
|
||||||
|
[239] = { "sendfile64", 4 },
|
||||||
|
[240] = { "futex", 6 },
|
||||||
|
[241] = { "sched_setaffinity", 3 },
|
||||||
|
[242] = { "sched_getaffinity", 3 },
|
||||||
|
[243] = { "set_thread_area", 6 },
|
||||||
|
[244] = { "get_thread_area", 6 },
|
||||||
|
[245] = { "io_setup", 2 },
|
||||||
|
[246] = { "io_destroy", 1 },
|
||||||
|
[247] = { "io_getevents", 5 },
|
||||||
|
[248] = { "io_submit", 3 },
|
||||||
|
[249] = { "io_cancel", 3 },
|
||||||
|
[250] = { "fadvise64", 4 },
|
||||||
|
[251] = { "not implemented", 0 },
|
||||||
|
[252] = { "exit_group", 1 },
|
||||||
|
[253] = { "lookup_dcookie", 3 },
|
||||||
|
[254] = { "epoll_create", 1 },
|
||||||
|
[255] = { "epoll_ctl", 4 },
|
||||||
|
[256] = { "epoll_wait", 4 },
|
||||||
|
[257] = { "remap_file_pages", 5 },
|
||||||
|
[258] = { "set_tid_address", 1 },
|
||||||
|
[259] = { "timer_create", 3 },
|
||||||
|
[260] = { "timer_settime", 4 },
|
||||||
|
[261] = { "timer_gettime", 2 },
|
||||||
|
[262] = { "timer_getoverrun", 1 },
|
||||||
|
[263] = { "timer_delete", 1 },
|
||||||
|
[264] = { "clock_settime", 2 },
|
||||||
|
[265] = { "clock_gettime", 2 },
|
||||||
|
[266] = { "clock_getres", 2 },
|
||||||
|
[267] = { "clock_nanosleep", 4 },
|
||||||
|
[268] = { "statfs64", 3 },
|
||||||
|
[269] = { "fstatfs64", 3 },
|
||||||
|
[270] = { "tgkill", 3 },
|
||||||
|
[271] = { "utimes", 2 },
|
||||||
|
[272] = { "fadvise64_64", 4 },
|
||||||
|
[273] = { "vserver", 6 },
|
||||||
|
[274] = { "mbind", 6 },
|
||||||
|
[275] = { "get_mempolicy", 5 },
|
||||||
|
[276] = { "set_mempolicy", 3 },
|
||||||
|
[277] = { "mq_open", 4 },
|
||||||
|
[278] = { "mq_unlink", 1 },
|
||||||
|
[279] = { "mq_timedsend", 5 },
|
||||||
|
[280] = { "mq_timedreceive", 5 },
|
||||||
|
[281] = { "mq_notify", 2 },
|
||||||
|
[282] = { "mq_getsetattr", 3 },
|
||||||
|
[283] = { "kexec_load", 4 },
|
||||||
|
[284] = { "waitid", 5 },
|
||||||
|
[285] = { "not implemented", 0 },
|
||||||
|
[286] = { "add_key", 5 },
|
||||||
|
[287] = { "request_key", 4 },
|
||||||
|
[288] = { "keyctl", 5 },
|
||||||
|
[289] = { "ioprio_set", 3 },
|
||||||
|
[290] = { "ioprio_get", 2 },
|
||||||
|
[291] = { "inotify_init", 0 },
|
||||||
|
[292] = { "inotify_add_watch", 3 },
|
||||||
|
[293] = { "inotify_rm_watch", 2 },
|
||||||
|
[294] = { "migrate_pages", 4 },
|
||||||
|
[295] = { "openat", 4 },
|
||||||
|
[296] = { "mkdirat", 3 },
|
||||||
|
[297] = { "mknodat", 4 },
|
||||||
|
[298] = { "fchownat", 5 },
|
||||||
|
[299] = { "futimesat", 3 },
|
||||||
|
[300] = { "fstatat64", 4 },
|
||||||
|
[301] = { "unlinkat", 3 },
|
||||||
|
[302] = { "renameat", 4 },
|
||||||
|
[303] = { "linkat", 5 },
|
||||||
|
[304] = { "symlinkat", 3 },
|
||||||
|
[305] = { "readlinkat", 4 },
|
||||||
|
[306] = { "fchmodat", 3 },
|
||||||
|
[307] = { "faccessat", 3 },
|
||||||
|
[308] = { "pselect6", 6 },
|
||||||
|
[309] = { "ppoll", 5 },
|
||||||
|
[310] = { "unshare", 1 },
|
||||||
|
[311] = { "set_robust_list", 2 },
|
||||||
|
[312] = { "get_robust_list", 3 },
|
||||||
|
[313] = { "splice", 6 },
|
||||||
|
[314] = { "sync_file_range", 4 },
|
||||||
|
[315] = { "tee", 4 },
|
||||||
|
[316] = { "vmsplice", 4 },
|
||||||
|
[317] = { "move_pages", 6 },
|
||||||
|
[318] = { "getcpu", 3 },
|
||||||
|
[319] = { "epoll_pwait", 6 },
|
||||||
|
[320] = { "utimensat", 4 },
|
||||||
|
[321] = { "signalfd", 3 },
|
||||||
|
[322] = { "timerfd_create", 2 },
|
||||||
|
[323] = { "eventfd", 1 },
|
||||||
|
[324] = { "fallocate", 4 },
|
||||||
|
[325] = { "timerfd_settime", 4 },
|
||||||
|
[326] = { "timerfd_gettime", 2 },
|
||||||
|
[327] = { "signalfd4", 4 },
|
||||||
|
[328] = { "eventfd2", 2 },
|
||||||
|
[329] = { "epoll_create1", 1 },
|
||||||
|
[330] = { "dup3", 3 },
|
||||||
|
[331] = { "pipe2", 2 },
|
||||||
|
[332] = { "inotify_init1", 1 },
|
||||||
|
[333] = { "preadv", 5 },
|
||||||
|
[334] = { "pwritev", 5 },
|
||||||
|
[335] = { "rt_tgsigqueueinfo", 4 },
|
||||||
|
[336] = { "perf_event_open", 5 },
|
||||||
|
[337] = { "recvmmsg", 5 },
|
||||||
|
[338] = { "fanotify_init", 2 },
|
||||||
|
[339] = { "fanotify_mark", 5 },
|
||||||
|
[340] = { "prlimit64", 4 },
|
||||||
|
[341] = { "name_to_handle_at", 5 },
|
||||||
|
[342] = { "open_by_handle_at", 3 },
|
||||||
|
[343] = { "clock_adjtime", 2 },
|
||||||
|
[344] = { "syncfs", 1 },
|
||||||
|
[345] = { "sendmmsg", 4 },
|
||||||
|
[346] = { "setns", 2 },
|
||||||
|
[347] = { "process_vm_readv", 6 },
|
||||||
|
[348] = { "process_vm_writev", 6 },
|
||||||
|
[349] = { "kcmp", 5 },
|
||||||
|
[350] = { "finit_module", 3 },
|
||||||
|
[351] = { "sched_setattr", 3 },
|
||||||
|
[352] = { "sched_getattr", 4 },
|
||||||
|
[353] = { "renameat2", 5 },
|
||||||
|
[354] = { "seccomp", 3 },
|
||||||
|
[355] = { "getrandom", 3 },
|
||||||
|
[356] = { "memfd_create", 2 },
|
||||||
|
[357] = { "bpf", 3 },
|
||||||
|
[358] = { "execveat", 5 },
|
||||||
|
[359] = { "socket", 3 },
|
||||||
|
[360] = { "socketpair", 4 },
|
||||||
|
[361] = { "bind", 3 },
|
||||||
|
[362] = { "connect", 3 },
|
||||||
|
[363] = { "listen", 2 },
|
||||||
|
[364] = { "accept4", 4 },
|
||||||
|
[365] = { "getsockopt", 5 },
|
||||||
|
[366] = { "setsockopt", 5 },
|
||||||
|
[367] = { "getsockname", 3 },
|
||||||
|
[368] = { "getpeername", 3 },
|
||||||
|
[369] = { "sendto", 6 },
|
||||||
|
[370] = { "sendmsg", 3 },
|
||||||
|
[371] = { "recvfrom", 6 },
|
||||||
|
[372] = { "recvmsg", 3 },
|
||||||
|
[373] = { "shutdown", 2 },
|
||||||
|
[374] = { "userfaultfd", 1 },
|
||||||
|
[375] = { "membarrier", 2 },
|
||||||
|
[376] = { "mlock2", 3 },
|
||||||
|
[377] = { "copy_file_range", 6 },
|
||||||
|
[378] = { "preadv2", 6 },
|
||||||
|
[379] = { "pwritev2", 6 },
|
||||||
|
[380] = { "pkey_mprotect", 4 },
|
||||||
|
[381] = { "pkey_alloc", 2 },
|
||||||
|
[382] = { "pkey_free", 1 },
|
||||||
|
[383] = { "statx", 5 },
|
||||||
|
[384] = { "arch_prctl", 6 },
|
||||||
|
};
|
||||||
|
|
||||||
|
const size_t g_syscalls_32_len =
|
||||||
|
sizeof(g_syscalls_64) / sizeof(g_syscalls_64[0]);
|
||||||
79
libft/Makefile
Executable file
79
libft/Makefile
Executable file
|
|
@ -0,0 +1,79 @@
|
||||||
|
CC = cc
|
||||||
|
|
||||||
|
CFLAGS = -Wall -Wextra -Werror -O3 -g3 -fPIC
|
||||||
|
|
||||||
|
SRCS = ./ft_isalnum.c ./ft_isalpha.c ./ft_isascii.c ./ft_isdigit.c \
|
||||||
|
./ft_isprint.c ./ft_memset.c ./ft_strlen.c ./ft_bzero.c ./ft_memcpy.c \
|
||||||
|
./ft_memmove.c ./ft_strlcpy.c ./ft_strlcat.c ./ft_toupper.c \
|
||||||
|
./ft_tolower.c ./ft_strchr.c ./ft_strrchr.c ./ft_strncmp.c \
|
||||||
|
./ft_memchr.c ./ft_memcmp.c ./ft_strnstr.c ./ft_atoi.c \
|
||||||
|
./ft_strdup.c ./ft_calloc.c ./ft_substr.c ./ft_strjoin.c \
|
||||||
|
./ft_strtrim.c ./ft_split.c ./ft_itoa.c ./ft_strmapi.c \
|
||||||
|
./ft_striteri.c ./ft_putchar_fd.c ./ft_putstr_fd.c \
|
||||||
|
./ft_putendl_fd.c ./ft_putnbr_fd.c ./ft_int_utils.c \
|
||||||
|
./ft_safe_int_math.c ./ft_split2.c ./ft_long_utils.c \
|
||||||
|
./ft_safe_long_math.c
|
||||||
|
|
||||||
|
SRCSBONUS = ./ft_lstnew_bonus.c ./ft_lstadd_front_bonus.c ./ft_lstsize_bonus.c ./ft_lstlast_bonus.c ./ft_lstadd_back_bonus.c ./ft_lstdelone_bonus.c ./ft_lstclear_bonus.c ./ft_lstiter_bonus.c ./ft_lstmap_bonus.c ./ft_vector.c ./ft_itoa_base.c ./ft_addr_to_str.c ./ft_utoa.c ./ft_strnonchr.c ./ft_utoa_base.c ./ft_strcmp.c ./ft_vecint.c ./ft_power.c
|
||||||
|
|
||||||
|
SRCS_GNL = ./get_next_line/get_next_line.c ./get_next_line/get_next_line_utils.c ./get_next_line/get_next_line_utils_two.c
|
||||||
|
|
||||||
|
FT_PRINTF_PATH = ./ft_printf/
|
||||||
|
SRC_PRINTF = ft_printf.c ft_cases_mandatory_one.c ft_cases_mandatory_two.c ft_printf_bonus.c ft_cases_easy_bonus.c ft_case_lowx_bonus.c ft_case_uppx_bonus.c ft_case_u_bonus.c ft_case_s_bonus.c ft_case_p_bonus.c ft_case_d_bonus.c ft_case_d_utils_bonus.c ft_arglist_bonus.c ft_padding_bonus.c
|
||||||
|
SRCS_PRINTF = $(addprefix $(FT_PRINTF_PATH), $(SRC_PRINTF))
|
||||||
|
|
||||||
|
FT_HASHMAP_PATH = ./ft_hashmap/
|
||||||
|
SRC_HASHMAP = ft_hashmap.c ft_hashmap_utils.c ft_hashmap_utils_two.c ft_hashmap_basic_type_hash.c ft_hashmap_advanced_type_hash.c ft_hashmap_basic_cmp.c
|
||||||
|
SRCS_HASHMAP = $(addprefix $(FT_HASHMAP_PATH), $(SRC_HASHMAP))
|
||||||
|
|
||||||
|
FT_PQ_PATH = ./ft_priority_queue/
|
||||||
|
SRC_PQ = ft_priority_queue.c ft_priority_queue_utils.c
|
||||||
|
SRCS_PQ = $(addprefix $(FT_PQ_PATH), $(SRC_PQ))
|
||||||
|
|
||||||
|
FT_SORTING_PATH = ./sorting/
|
||||||
|
SRC_SORTING = ft_radixsort.c ft_sorting.c
|
||||||
|
SRCS_SORTING = $(addprefix $(FT_SORTING_PATH), $(SRC_SORTING))
|
||||||
|
|
||||||
|
OBJS = $(SRCS:.c=.o)
|
||||||
|
|
||||||
|
OBJSBONUS = $(SRCSBONUS:.c=.o)
|
||||||
|
|
||||||
|
OBJS_GNL = $(SRCS_GNL:.c=.o)
|
||||||
|
|
||||||
|
OBJS_PRINTF = $(SRCS_PRINTF:.c=.o)
|
||||||
|
|
||||||
|
OBJS_HASHMAP = $(SRCS_HASHMAP:.c=.o)
|
||||||
|
|
||||||
|
OBJS_PQ = $(SRCS_PQ:.c=.o)
|
||||||
|
|
||||||
|
OBJS_SORTING = $(SRCS_SORTING:.c=.o)
|
||||||
|
|
||||||
|
NAME = libft.a
|
||||||
|
|
||||||
|
RED := \033[0;31m
|
||||||
|
GREEN := \033[0;32m
|
||||||
|
YELLOW := \033[0;33m
|
||||||
|
BLUE := \033[0;34m
|
||||||
|
NC := \033[0m
|
||||||
|
|
||||||
|
|
||||||
|
all: $(NAME)
|
||||||
|
|
||||||
|
$(NAME): $(OBJS) $(OBJSBONUS) $(OBJS_GNL) $(OBJS_PRINTF) $(OBJS_HASHMAP) $(OBJS_PQ) $(OBJS_SORTING)
|
||||||
|
@ar rcs $(NAME) $(OBJS) $(OBJSBONUS) $(OBJS_GNL) $(OBJS_PRINTF) $(OBJS_HASHMAP) $(OBJS_PQ) $(OBJS_SORTING)
|
||||||
|
@echo "$(GREEN)finished compiling $(NAME)!$(NC)"
|
||||||
|
|
||||||
|
bonus: $(NAME)
|
||||||
|
|
||||||
|
./%.o: ./%.c
|
||||||
|
@$(CC) $(CFLAGS) -c $< -o $@
|
||||||
|
|
||||||
|
clean:
|
||||||
|
@rm -f $(OBJS) $(OBJSBONUS) $(OBJS_GNL) $(OBJS_PRINTF) $(OBJS_HASHMAP) $(OBJS_PQ) $(OBJS_SORTING)
|
||||||
|
@echo "$(YELLOW)deleted object files from $(NAME)$(NC)"
|
||||||
|
|
||||||
|
fclean: clean
|
||||||
|
@rm -f $(NAME)
|
||||||
|
@echo "$(YELLOW)deleted $(NAME)$(NC)"
|
||||||
|
|
||||||
|
re: fclean all
|
||||||
55
libft/ft_addr_to_str.c
Executable file
55
libft/ft_addr_to_str.c
Executable file
|
|
@ -0,0 +1,55 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_addr_to_str.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/10/22 16:54:09 by thrieg #+# #+# */
|
||||||
|
/* Updated: 2025/02/16 19:02:50 by thrieg ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
void ft_convert_to_hexa(char c, char hex_buff[2])
|
||||||
|
{
|
||||||
|
hex_buff[1] = c & 0b00001111;
|
||||||
|
hex_buff[0] = (c >> 4) & 0b00001111;
|
||||||
|
if (hex_buff[0] <= 9)
|
||||||
|
hex_buff[0] += '0';
|
||||||
|
else
|
||||||
|
hex_buff[0] += 'a' - 10;
|
||||||
|
if (hex_buff[1] <= 9)
|
||||||
|
hex_buff[1] += '0';
|
||||||
|
else
|
||||||
|
hex_buff[1] += 'a' - 10;
|
||||||
|
}
|
||||||
|
|
||||||
|
//convert the address addr in hexadecimal and returns a standard c string.
|
||||||
|
//returns NULL if the allocation fails
|
||||||
|
char *ft_addr_to_strhex(void *addr)
|
||||||
|
{
|
||||||
|
char hex_buff[2];
|
||||||
|
int i;
|
||||||
|
int j;
|
||||||
|
uintptr_t address;
|
||||||
|
char *ret;
|
||||||
|
|
||||||
|
ret = malloc((sizeof(char) * sizeof(uintptr_t) * 2) + 1);
|
||||||
|
if (!ret)
|
||||||
|
return (NULL);
|
||||||
|
address = (uintptr_t)addr;
|
||||||
|
i = (sizeof(uintptr_t) * 8) - 8;
|
||||||
|
j = 0;
|
||||||
|
while (i >= 0)
|
||||||
|
{
|
||||||
|
ft_convert_to_hexa(((address >> i) & 0xFF), hex_buff);
|
||||||
|
ret[j++] = hex_buff[0];
|
||||||
|
ret[j++] = hex_buff[1];
|
||||||
|
i -= 8;
|
||||||
|
}
|
||||||
|
ret[j] = '\0';
|
||||||
|
return (ret);
|
||||||
|
}
|
||||||
BIN
libft/ft_addr_to_str.o
Normal file
BIN
libft/ft_addr_to_str.o
Normal file
Binary file not shown.
71
libft/ft_atoi.c
Executable file
71
libft/ft_atoi.c
Executable file
|
|
@ -0,0 +1,71 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_atoi.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/10/14 18:37:49 by thrieg #+# #+# */
|
||||||
|
/* Updated: 2025/02/16 19:02:53 by thrieg ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
|
static int ft_is_space(char c)
|
||||||
|
{
|
||||||
|
if (c == '\t' || c == '\n' || c == '\v'
|
||||||
|
|| c == '\f' || c == '\r' || c == ' ')
|
||||||
|
return (1);
|
||||||
|
else
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int ft_convert_to_int(const char *str, int sign, int *overflow_flag)
|
||||||
|
{
|
||||||
|
int value;
|
||||||
|
|
||||||
|
value = 0;
|
||||||
|
while (ft_isdigit(*str))
|
||||||
|
{
|
||||||
|
if (sign == 1)
|
||||||
|
{
|
||||||
|
if (value > (INT_MAX - (*str - '0')) / 10)
|
||||||
|
if (overflow_flag)
|
||||||
|
*overflow_flag = 1;
|
||||||
|
value = value * 10 + (*str - '0');
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (value < (INT_MIN + (*str - '0')) / 10)
|
||||||
|
if (overflow_flag)
|
||||||
|
*overflow_flag = 1;
|
||||||
|
value = value * 10 - (*str - '0');
|
||||||
|
}
|
||||||
|
str++;
|
||||||
|
}
|
||||||
|
return (value);
|
||||||
|
}
|
||||||
|
|
||||||
|
int ft_atoi(const char *nptr, int *overflow_flag)
|
||||||
|
{
|
||||||
|
int sign;
|
||||||
|
|
||||||
|
if (overflow_flag)
|
||||||
|
*overflow_flag = 0;
|
||||||
|
while ((*nptr != '\0') && ft_is_space(*nptr))
|
||||||
|
{
|
||||||
|
nptr++;
|
||||||
|
}
|
||||||
|
sign = 1;
|
||||||
|
if ((*nptr != '\0') && (*nptr == '-' || *nptr == '+'))
|
||||||
|
{
|
||||||
|
if (*nptr == '-')
|
||||||
|
sign = -sign;
|
||||||
|
nptr++;
|
||||||
|
}
|
||||||
|
if (!ft_isdigit(*nptr))
|
||||||
|
return (0);
|
||||||
|
return (ft_convert_to_int(nptr, sign, overflow_flag));
|
||||||
|
}
|
||||||
BIN
libft/ft_atoi.o
Normal file
BIN
libft/ft_atoi.o
Normal file
Binary file not shown.
18
libft/ft_bzero.c
Executable file
18
libft/ft_bzero.c
Executable file
|
|
@ -0,0 +1,18 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_bzero.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/10/14 15:50:09 by thrieg #+# #+# */
|
||||||
|
/* Updated: 2025/02/16 19:03:30 by thrieg ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
void ft_bzero(void *s, size_t n)
|
||||||
|
{
|
||||||
|
ft_memset(s, '\0', n);
|
||||||
|
}
|
||||||
BIN
libft/ft_bzero.o
Normal file
BIN
libft/ft_bzero.o
Normal file
Binary file not shown.
30
libft/ft_calloc.c
Executable file
30
libft/ft_calloc.c
Executable file
|
|
@ -0,0 +1,30 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_calloc.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/10/14 19:42:05 by thrieg #+# #+# */
|
||||||
|
/* Updated: 2025/02/16 19:03:37 by thrieg ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
void *ft_calloc(size_t nmemb, size_t size)
|
||||||
|
{
|
||||||
|
void *ret;
|
||||||
|
|
||||||
|
if ((size == 0) || (nmemb == 0))
|
||||||
|
{
|
||||||
|
return (malloc(0));
|
||||||
|
}
|
||||||
|
if (nmemb > __SIZE_MAX__ / size)
|
||||||
|
return (NULL);
|
||||||
|
ret = malloc(nmemb * size);
|
||||||
|
if (!ret)
|
||||||
|
return (NULL);
|
||||||
|
ft_bzero(ret, (nmemb * size));
|
||||||
|
return (ret);
|
||||||
|
}
|
||||||
BIN
libft/ft_calloc.o
Normal file
BIN
libft/ft_calloc.o
Normal file
Binary file not shown.
93
libft/ft_hashmap.h
Executable file
93
libft/ft_hashmap.h
Executable file
|
|
@ -0,0 +1,93 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_hashmap.h :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/11/07 12:14:47 by thrieg #+# #+# */
|
||||||
|
/* Updated: 2025/02/16 19:06:02 by thrieg ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#ifndef FT_HASHMAP_H
|
||||||
|
# define FT_HASHMAP_H
|
||||||
|
|
||||||
|
# include "libft.h"
|
||||||
|
# include "ft_vector.h"
|
||||||
|
|
||||||
|
# define HASHMAP_FILL_PERCENT_MAX 200
|
||||||
|
# define HASHMAP_RESIZE_FACTOR 2
|
||||||
|
# define HASHMAP_MINIMUM_SIZE 16
|
||||||
|
# define HASHMAP_FILL_PERCENT_BEFORE_SHRINK 50
|
||||||
|
# define HASHMAP_SHRINK_FACTOR 2
|
||||||
|
|
||||||
|
typedef struct s_hashmap
|
||||||
|
{
|
||||||
|
t_list **table;
|
||||||
|
size_t hashmap_size;
|
||||||
|
size_t nb_elems;
|
||||||
|
void (*free_key)(void *key);
|
||||||
|
void (*free_value)(void *value);
|
||||||
|
int (*cmp)(void *key, void *key_to_compare);
|
||||||
|
} t_hashmap;
|
||||||
|
|
||||||
|
size_t ft_hash_int(int value);
|
||||||
|
size_t ft_hash_uint(unsigned int value);
|
||||||
|
size_t ft_hash_char(char value);
|
||||||
|
size_t ft_hash_string(const char *str);
|
||||||
|
size_t ft_hash_size_t(size_t value);
|
||||||
|
size_t ft_hash_address(const void *key);
|
||||||
|
size_t ft_hash_list(const t_list *list);
|
||||||
|
size_t ft_hash_hashmap(const t_hashmap *hashmap);
|
||||||
|
size_t ft_hash_vector(const t_vector *vec);
|
||||||
|
// Comparison function for integers
|
||||||
|
int int_cmp(void *key1, void *key2);
|
||||||
|
// Comparison function for strings
|
||||||
|
int cmp_str(void *key1, void *key2);
|
||||||
|
// Comparison function for unsigned chars
|
||||||
|
int cmp_uchar(void *key1, void *key2);
|
||||||
|
// Comparison function for unsigned integers
|
||||||
|
int uint_cmp(void *key1, void *key2);
|
||||||
|
// Comparison function for size_t
|
||||||
|
int sizet_cmp(void *key1, void *key2);
|
||||||
|
|
||||||
|
//returns 0 without doing anything if the allocation of the new table fails
|
||||||
|
//in that case the old table is still valid!
|
||||||
|
//returns 1 if the resize is a success
|
||||||
|
int ft_resize_hashmap(t_hashmap *map);
|
||||||
|
|
||||||
|
//returns 0 without doing anything if the allocation of the new table fails
|
||||||
|
//or if the table is too little in that case the old table is still valid!
|
||||||
|
//returns 1 if the shrink is a success
|
||||||
|
int ft_shrink_hashmap(t_hashmap *map);
|
||||||
|
|
||||||
|
//applies functions free_key and free_value on the element if they are not NULL
|
||||||
|
//returns 1 if the element has been deleted
|
||||||
|
//returns 0 if the element has not been found
|
||||||
|
int ft_delete_hashmap_element(t_hashmap *map, void *key, size_t hash);
|
||||||
|
|
||||||
|
//returns a pointer to the value of key, or a NULL pointer if not found
|
||||||
|
void *ft_lookup_hashmap_element(t_hashmap *map, void *key, size_t hash);
|
||||||
|
|
||||||
|
//frees the entire hashmap and then returns NULL once done
|
||||||
|
void *ft_free_hashmap(t_hashmap *map);
|
||||||
|
|
||||||
|
// Inserts a key-value pair into the hashmap,
|
||||||
|
//returns 0 for an error, 1 for remplaced value for a key and 2 for new key
|
||||||
|
int ft_hashmap_insert_element(
|
||||||
|
t_hashmap *map,
|
||||||
|
void *key,
|
||||||
|
void *value,
|
||||||
|
size_t hash);
|
||||||
|
|
||||||
|
//allocates an array of initial_size, and sets nb_elems/hashmap_size to 0
|
||||||
|
//cmp is a function that returns 0 if both keys are the same
|
||||||
|
//returns NULL if any allocation fails
|
||||||
|
t_hashmap *ft_create_hashmap(
|
||||||
|
size_t initial_size,
|
||||||
|
void (*free_key)(void *),
|
||||||
|
void (*free_value)(void *),
|
||||||
|
int (*cmp)(void *key, void *key_to_compare));
|
||||||
|
|
||||||
|
#endif
|
||||||
156
libft/ft_hashmap/ft_hashmap.c
Executable file
156
libft/ft_hashmap/ft_hashmap.c
Executable file
|
|
@ -0,0 +1,156 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_hashmap.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/11/07 12:14:50 by thrieg #+# #+# */
|
||||||
|
/* Updated: 2025/02/16 19:08:34 by thrieg ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "ft_hashmap_private.h"
|
||||||
|
|
||||||
|
//allocates an array of initial_size, and sets nb_elems/hashmap_size to 0
|
||||||
|
//cmp is a function that returns 0 if both keys are the same
|
||||||
|
//returns NULL if any allocation fails
|
||||||
|
t_hashmap *ft_create_hashmap(
|
||||||
|
size_t initial_size,
|
||||||
|
void (*free_key)(void *),
|
||||||
|
void (*free_value)(void *),
|
||||||
|
int (*cmp)(void *key, void *key_to_compare))
|
||||||
|
{
|
||||||
|
t_hashmap *map;
|
||||||
|
|
||||||
|
if (initial_size == 0)
|
||||||
|
return (NULL);
|
||||||
|
map = malloc(sizeof(t_hashmap));
|
||||||
|
if (!map)
|
||||||
|
return (NULL);
|
||||||
|
map->nb_elems = 0;
|
||||||
|
map->hashmap_size = initial_size;
|
||||||
|
map->cmp = cmp;
|
||||||
|
map->free_key = free_key;
|
||||||
|
map->free_value = free_value;
|
||||||
|
map->table = ft_calloc((initial_size), sizeof(t_list *));
|
||||||
|
if (!(map->table))
|
||||||
|
return (free(map), NULL);
|
||||||
|
return (map);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Inserts a key-value pair into the hashmap,
|
||||||
|
//returns 0 for an error, 1 for remplaced value for a key and 2 for new key
|
||||||
|
int ft_hashmap_insert_element(
|
||||||
|
t_hashmap *map,
|
||||||
|
void *key,
|
||||||
|
void *value,
|
||||||
|
size_t hash)
|
||||||
|
{
|
||||||
|
size_t index;
|
||||||
|
t_list *current;
|
||||||
|
t_hash_element *element;
|
||||||
|
|
||||||
|
if (!map || !key)
|
||||||
|
return (0);
|
||||||
|
if ((map->nb_elems * 100) > map->hashmap_size * HASHMAP_FILL_PERCENT_MAX)
|
||||||
|
ft_resize_hashmap(map);
|
||||||
|
index = hash % map->hashmap_size;
|
||||||
|
current = map->table[index];
|
||||||
|
while (current)
|
||||||
|
{
|
||||||
|
if (map->cmp(((t_hash_element *)(current->content))->key, key) == 0)
|
||||||
|
{
|
||||||
|
map->free_value(((t_hash_element *)(current->content))->value);
|
||||||
|
return (((t_hash_element *)(current->content))->value = value, 1);
|
||||||
|
}
|
||||||
|
current = current->next;
|
||||||
|
}
|
||||||
|
element = ft_create_hash_element(key, value, hash);
|
||||||
|
if (!element)
|
||||||
|
return (0);
|
||||||
|
if (!ft_add_hashmap_element(index, map, element))
|
||||||
|
return (ft_free_hash_element(element, map), 0);
|
||||||
|
return (2);
|
||||||
|
}
|
||||||
|
|
||||||
|
//returns 1 if the element has been deleted
|
||||||
|
//returns 0 if the element has not been found
|
||||||
|
int ft_delete_hashmap_element(t_hashmap *map, void *key, size_t hash)
|
||||||
|
{
|
||||||
|
size_t index;
|
||||||
|
t_list *curr;
|
||||||
|
t_list *prev;
|
||||||
|
t_hash_element *elem;
|
||||||
|
|
||||||
|
if (!map || !key || map->hashmap_size == 0)
|
||||||
|
return (0);
|
||||||
|
if (((map->nb_elems * 100) / map->hashmap_size)
|
||||||
|
<= HASHMAP_FILL_PERCENT_BEFORE_SHRINK)
|
||||||
|
ft_shrink_hashmap(map);
|
||||||
|
index = hash % map->hashmap_size;
|
||||||
|
curr = map->table[index];
|
||||||
|
prev = NULL;
|
||||||
|
while (curr)
|
||||||
|
{
|
||||||
|
elem = (t_hash_element *)curr->content;
|
||||||
|
if (map->cmp(elem->key, key) == 0)
|
||||||
|
{
|
||||||
|
return (ft_delete_hashmap_list_node(curr, prev, index, map), 1);
|
||||||
|
}
|
||||||
|
prev = curr;
|
||||||
|
curr = curr->next;
|
||||||
|
}
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
//returns a pointer to the value of key, or a NULL pointer if not found
|
||||||
|
void *ft_lookup_hashmap_element(t_hashmap *map, void *key, size_t hash)
|
||||||
|
{
|
||||||
|
size_t index;
|
||||||
|
t_list *curr;
|
||||||
|
t_hash_element *elem;
|
||||||
|
|
||||||
|
if (!map || !key || map->hashmap_size == 0)
|
||||||
|
return (NULL);
|
||||||
|
index = hash % map->hashmap_size;
|
||||||
|
curr = map->table[index];
|
||||||
|
while (curr)
|
||||||
|
{
|
||||||
|
elem = (t_hash_element *)curr->content;
|
||||||
|
if (map->cmp(elem->key, key) == 0)
|
||||||
|
return (elem->value);
|
||||||
|
curr = curr->next;
|
||||||
|
}
|
||||||
|
return (NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
//frees the entire hashmap and then returns NULL once done
|
||||||
|
void *ft_free_hashmap(t_hashmap *map)
|
||||||
|
{
|
||||||
|
size_t index;
|
||||||
|
t_list *curr;
|
||||||
|
t_list *next;
|
||||||
|
t_hash_element *elem;
|
||||||
|
|
||||||
|
if (!map)
|
||||||
|
return (NULL);
|
||||||
|
if (!map->table)
|
||||||
|
return (free(map), NULL);
|
||||||
|
index = 0;
|
||||||
|
while (index < map->hashmap_size)
|
||||||
|
{
|
||||||
|
curr = map->table[index];
|
||||||
|
while (curr)
|
||||||
|
{
|
||||||
|
next = curr->next;
|
||||||
|
elem = (t_hash_element *)curr->content;
|
||||||
|
ft_free_hash_element(elem, map);
|
||||||
|
free(curr);
|
||||||
|
curr = next;
|
||||||
|
}
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
free(map->table);
|
||||||
|
return (free(map), NULL);
|
||||||
|
}
|
||||||
BIN
libft/ft_hashmap/ft_hashmap.o
Normal file
BIN
libft/ft_hashmap/ft_hashmap.o
Normal file
Binary file not shown.
57
libft/ft_hashmap/ft_hashmap_advanced_type_hash.c
Executable file
57
libft/ft_hashmap/ft_hashmap_advanced_type_hash.c
Executable file
|
|
@ -0,0 +1,57 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_hashmap_advanced_type_hash.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/11/07 12:47:23 by thrieg #+# #+# */
|
||||||
|
/* Updated: 2025/02/16 19:08:26 by thrieg ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "ft_hashmap_private.h"
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
//hash function for an address, returns a size_t between 0 and size_t max
|
||||||
|
size_t ft_hash_address(const void *key)
|
||||||
|
{
|
||||||
|
uintptr_t address;
|
||||||
|
|
||||||
|
address = (uintptr_t)key;
|
||||||
|
return ((size_t)address >> 3);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Hash function for a t_list, returns a size_t between 0 and size_t max
|
||||||
|
//takes the function to hash a single element
|
||||||
|
size_t ft_hash_list(const t_list *list)
|
||||||
|
{
|
||||||
|
return (ft_hash_address(list));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Hash function for a t_hashmap, returns a size_t between 0 and size_t max
|
||||||
|
size_t ft_hash_hashmap(const t_hashmap *hashmap)
|
||||||
|
{
|
||||||
|
return (ft_hash_address(hashmap));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Hash function for a t_vector, returns a size_t between 0 and size_t max
|
||||||
|
size_t ft_hash_vector(const t_vector *vec)
|
||||||
|
{
|
||||||
|
size_t i;
|
||||||
|
int c;
|
||||||
|
size_t index;
|
||||||
|
size_t hash;
|
||||||
|
char *str;
|
||||||
|
|
||||||
|
hash = 5381;
|
||||||
|
index = vec->index;
|
||||||
|
str = vec->buffer;
|
||||||
|
i = 0;
|
||||||
|
while (i < index)
|
||||||
|
{
|
||||||
|
c = str[i++];
|
||||||
|
hash = ((hash << 5) + hash) + c;
|
||||||
|
}
|
||||||
|
return (hash);
|
||||||
|
}
|
||||||
BIN
libft/ft_hashmap/ft_hashmap_advanced_type_hash.o
Normal file
BIN
libft/ft_hashmap/ft_hashmap_advanced_type_hash.o
Normal file
Binary file not shown.
79
libft/ft_hashmap/ft_hashmap_basic_cmp.c
Executable file
79
libft/ft_hashmap/ft_hashmap_basic_cmp.c
Executable file
|
|
@ -0,0 +1,79 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_hashmap_basic_cmp.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/11/08 13:18:52 by thrieg #+# #+# */
|
||||||
|
/* Updated: 2025/02/16 19:08:27 by thrieg ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "ft_hashmap_private.h"
|
||||||
|
#include <limits.h>
|
||||||
|
|
||||||
|
// Comparison function for integers
|
||||||
|
int int_cmp(void *key1, void *key2)
|
||||||
|
{
|
||||||
|
int a;
|
||||||
|
int b;
|
||||||
|
|
||||||
|
a = *(int *)key1;
|
||||||
|
b = *(int *)key2;
|
||||||
|
if (a < b)
|
||||||
|
return (-1);
|
||||||
|
else if (a > b)
|
||||||
|
return (1);
|
||||||
|
else
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Comparison function for strings
|
||||||
|
int cmp_str(void *key1, void *key2)
|
||||||
|
{
|
||||||
|
return (ft_strcmp((char *)key1, (char *)key2));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Comparison function for unsigned chars
|
||||||
|
int cmp_uchar(void *key1, void *key2)
|
||||||
|
{
|
||||||
|
unsigned char *c1;
|
||||||
|
unsigned char *c2;
|
||||||
|
|
||||||
|
c1 = (unsigned char *)key1;
|
||||||
|
c2 = (unsigned char *)key2;
|
||||||
|
return (*c1 - *c2);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Comparison function for unsigned integers
|
||||||
|
int uint_cmp(void *key1, void *key2)
|
||||||
|
{
|
||||||
|
unsigned int a;
|
||||||
|
unsigned int b;
|
||||||
|
|
||||||
|
a = *(unsigned int *)key1;
|
||||||
|
b = *(unsigned int *)key2;
|
||||||
|
if (a < b)
|
||||||
|
return (-1);
|
||||||
|
else if (a > b)
|
||||||
|
return (1);
|
||||||
|
else
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Comparison function for size_t
|
||||||
|
int sizet_cmp(void *key1, void *key2)
|
||||||
|
{
|
||||||
|
size_t a;
|
||||||
|
size_t b;
|
||||||
|
|
||||||
|
a = *(size_t *)key1;
|
||||||
|
b = *(size_t *)key2;
|
||||||
|
if (a < b)
|
||||||
|
return (-1);
|
||||||
|
else if (a > b)
|
||||||
|
return (1);
|
||||||
|
else
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
BIN
libft/ft_hashmap/ft_hashmap_basic_cmp.o
Normal file
BIN
libft/ft_hashmap/ft_hashmap_basic_cmp.o
Normal file
Binary file not shown.
57
libft/ft_hashmap/ft_hashmap_basic_type_hash.c
Executable file
57
libft/ft_hashmap/ft_hashmap_basic_type_hash.c
Executable file
|
|
@ -0,0 +1,57 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_hashmap_basic_type_hash.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/11/07 12:34:20 by thrieg #+# #+# */
|
||||||
|
/* Updated: 2025/02/16 19:08:29 by thrieg ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "ft_hashmap_private.h"
|
||||||
|
|
||||||
|
// Integer hash function, returns a size_t between 0 and size_t max
|
||||||
|
size_t ft_hash_int(int value)
|
||||||
|
{
|
||||||
|
return ((size_t)(value * 2654435761u));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unsigned integer hash function, returns a size_t between 0 and size_t max
|
||||||
|
size_t ft_hash_uint(unsigned int value)
|
||||||
|
{
|
||||||
|
return ((size_t)(value * 2654435761u));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Character hash function, returns a size_t between 0 and size_t max
|
||||||
|
size_t ft_hash_char(char value)
|
||||||
|
{
|
||||||
|
return ((size_t)(value * 31));
|
||||||
|
}
|
||||||
|
|
||||||
|
// String hash function (DJB2), returns a size_t between 0 and size_t max
|
||||||
|
size_t ft_hash_string(const char *str)
|
||||||
|
{
|
||||||
|
int c;
|
||||||
|
size_t hash;
|
||||||
|
|
||||||
|
hash = 5381;
|
||||||
|
while (*str)
|
||||||
|
{
|
||||||
|
c = *str++;
|
||||||
|
hash = ((hash << 5) + hash) + c;
|
||||||
|
}
|
||||||
|
return (hash);
|
||||||
|
}
|
||||||
|
|
||||||
|
// size_t hash function, returns a size_t between 0 and size_t max
|
||||||
|
size_t ft_hash_size_t(size_t value)
|
||||||
|
{
|
||||||
|
value ^= value >> 33;
|
||||||
|
value *= 0xff51afd7ed558ccd;
|
||||||
|
value ^= value >> 33;
|
||||||
|
value *= 0xc4ceb9fe1a85ec53;
|
||||||
|
value ^= value >> 33;
|
||||||
|
return (value);
|
||||||
|
}
|
||||||
BIN
libft/ft_hashmap/ft_hashmap_basic_type_hash.o
Normal file
BIN
libft/ft_hashmap/ft_hashmap_basic_type_hash.o
Normal file
Binary file not shown.
46
libft/ft_hashmap/ft_hashmap_private.h
Executable file
46
libft/ft_hashmap/ft_hashmap_private.h
Executable file
|
|
@ -0,0 +1,46 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_hashmap_private.h :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/11/07 13:49:11 by thrieg #+# #+# */
|
||||||
|
/* Updated: 2025/02/16 19:08:30 by thrieg ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#ifndef FT_HASHMAP_PRIVATE_H
|
||||||
|
# define FT_HASHMAP_PRIVATE_H
|
||||||
|
|
||||||
|
# include "../ft_hashmap.h"
|
||||||
|
|
||||||
|
typedef struct s_hash_element
|
||||||
|
{
|
||||||
|
void *key;
|
||||||
|
void *value;
|
||||||
|
size_t hash;
|
||||||
|
} t_hash_element;
|
||||||
|
|
||||||
|
t_hash_element *ft_create_hash_element(void *key, void *value, size_t hash);
|
||||||
|
void ft_free_hash_element(t_hash_element *elem, t_hashmap *map);
|
||||||
|
int ft_add_hashmap_element(
|
||||||
|
size_t index,
|
||||||
|
t_hashmap *map,
|
||||||
|
t_hash_element *elem);
|
||||||
|
int ft_add_hashmap_element(
|
||||||
|
size_t index,
|
||||||
|
t_hashmap *map,
|
||||||
|
t_hash_element *elem);
|
||||||
|
void ft_copy_hashmap_table(
|
||||||
|
t_list **new_table,
|
||||||
|
t_list **old_table,
|
||||||
|
size_t new_size,
|
||||||
|
size_t old_size);
|
||||||
|
void ft_delete_hashmap_list_node(
|
||||||
|
t_list *curr,
|
||||||
|
t_list *prev,
|
||||||
|
size_t index,
|
||||||
|
t_hashmap *map);
|
||||||
|
|
||||||
|
#endif
|
||||||
70
libft/ft_hashmap/ft_hashmap_utils.c
Executable file
70
libft/ft_hashmap/ft_hashmap_utils.c
Executable file
|
|
@ -0,0 +1,70 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_hashmap_utils.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/11/07 13:51:48 by thrieg #+# #+# */
|
||||||
|
/* Updated: 2025/02/16 19:08:32 by thrieg ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "ft_hashmap_private.h"
|
||||||
|
|
||||||
|
void ft_free_hash_element(t_hash_element *elem, t_hashmap *map)
|
||||||
|
{
|
||||||
|
if (map->free_key)
|
||||||
|
map->free_key(elem->key);
|
||||||
|
if (map->free_value)
|
||||||
|
map->free_value(elem->value);
|
||||||
|
free(elem);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Helper function to create a new hash element
|
||||||
|
t_hash_element *ft_create_hash_element(void *key, void *value, size_t hash)
|
||||||
|
{
|
||||||
|
t_hash_element *new_element;
|
||||||
|
|
||||||
|
new_element = malloc(sizeof(t_hash_element));
|
||||||
|
if (!new_element)
|
||||||
|
return (NULL);
|
||||||
|
new_element->key = key;
|
||||||
|
new_element->value = value;
|
||||||
|
new_element->hash = hash;
|
||||||
|
return (new_element);
|
||||||
|
}
|
||||||
|
|
||||||
|
//returns 0 for allocation fail without allocating or freeing anything
|
||||||
|
int ft_add_hashmap_element(size_t index, t_hashmap *map, t_hash_element *elem)
|
||||||
|
{
|
||||||
|
t_list *new_node;
|
||||||
|
|
||||||
|
new_node = malloc(sizeof(t_list));
|
||||||
|
if (!new_node)
|
||||||
|
return (0);
|
||||||
|
new_node->content = elem;
|
||||||
|
new_node->next = map->table[index];
|
||||||
|
map->table[index] = new_node;
|
||||||
|
map->nb_elems++;
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
//returns 0 without doing anything if the allocation of the new table fails
|
||||||
|
//in that case the old table is still valid!
|
||||||
|
//returns 1 if the resize is a success
|
||||||
|
int ft_resize_hashmap(t_hashmap *map)
|
||||||
|
{
|
||||||
|
size_t new_size;
|
||||||
|
t_list **new_table;
|
||||||
|
|
||||||
|
new_size = map->hashmap_size * HASHMAP_RESIZE_FACTOR;
|
||||||
|
new_table = ft_calloc(new_size, sizeof(t_list *));
|
||||||
|
if (!new_table)
|
||||||
|
return (0);
|
||||||
|
ft_copy_hashmap_table(new_table, map->table, new_size, map->hashmap_size);
|
||||||
|
free(map->table);
|
||||||
|
map->table = new_table;
|
||||||
|
map->hashmap_size = new_size;
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
BIN
libft/ft_hashmap/ft_hashmap_utils.o
Normal file
BIN
libft/ft_hashmap/ft_hashmap_utils.o
Normal file
Binary file not shown.
85
libft/ft_hashmap/ft_hashmap_utils_two.c
Executable file
85
libft/ft_hashmap/ft_hashmap_utils_two.c
Executable file
|
|
@ -0,0 +1,85 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_hashmap_utils_two.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/11/07 16:04:36 by thrieg #+# #+# */
|
||||||
|
/* Updated: 2025/02/16 19:08:31 by thrieg ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "ft_hashmap_private.h"
|
||||||
|
|
||||||
|
//doesn't allocate anything no error possible
|
||||||
|
void ft_copy_hashmap_table(
|
||||||
|
t_list **new_table,
|
||||||
|
t_list **old_table,
|
||||||
|
size_t new_size,
|
||||||
|
size_t old_size)
|
||||||
|
{
|
||||||
|
t_list *curr;
|
||||||
|
t_list *next;
|
||||||
|
t_hash_element *elem;
|
||||||
|
size_t i;
|
||||||
|
size_t new_index;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (i < old_size)
|
||||||
|
{
|
||||||
|
curr = old_table[i];
|
||||||
|
while (curr)
|
||||||
|
{
|
||||||
|
next = curr->next;
|
||||||
|
elem = (t_hash_element *)curr->content;
|
||||||
|
new_index = elem->hash % new_size;
|
||||||
|
curr->next = new_table[new_index];
|
||||||
|
new_table[new_index] = curr;
|
||||||
|
curr = next;
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//helper function for delete_hashmap_element to delete the node once found
|
||||||
|
void ft_delete_hashmap_list_node(
|
||||||
|
t_list *curr,
|
||||||
|
t_list *prev,
|
||||||
|
size_t index,
|
||||||
|
t_hashmap *map)
|
||||||
|
{
|
||||||
|
t_hash_element *elem;
|
||||||
|
|
||||||
|
elem = (t_hash_element *)curr->content;
|
||||||
|
if (prev)
|
||||||
|
prev->next = curr->next;
|
||||||
|
else
|
||||||
|
map->table[index] = curr->next;
|
||||||
|
ft_free_hash_element(elem, map);
|
||||||
|
free(curr);
|
||||||
|
map->nb_elems--;
|
||||||
|
}
|
||||||
|
|
||||||
|
//returns 0 without doing anything if the allocation of the new table fails
|
||||||
|
//or if the table is too little in that case the old table is still valid!
|
||||||
|
//returns 1 if the shrink is a success
|
||||||
|
int ft_shrink_hashmap(t_hashmap *map)
|
||||||
|
{
|
||||||
|
size_t new_size;
|
||||||
|
t_list **new_table;
|
||||||
|
|
||||||
|
if (map->hashmap_size == HASHMAP_MINIMUM_SIZE)
|
||||||
|
return (0);
|
||||||
|
new_size = map->hashmap_size / HASHMAP_SHRINK_FACTOR;
|
||||||
|
if (new_size < HASHMAP_MINIMUM_SIZE)
|
||||||
|
new_size = HASHMAP_MINIMUM_SIZE;
|
||||||
|
new_table = ft_calloc(new_size, sizeof(t_list *));
|
||||||
|
if (!new_table)
|
||||||
|
return (0);
|
||||||
|
ft_copy_hashmap_table(new_table, map->table, new_size, map->hashmap_size);
|
||||||
|
free(map->table);
|
||||||
|
map->table = new_table;
|
||||||
|
map->hashmap_size = new_size;
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
BIN
libft/ft_hashmap/ft_hashmap_utils_two.o
Normal file
BIN
libft/ft_hashmap/ft_hashmap_utils_two.o
Normal file
Binary file not shown.
80
libft/ft_int_utils.c
Normal file
80
libft/ft_int_utils.c
Normal file
|
|
@ -0,0 +1,80 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_int_utils.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/10/30 10:11:17 by alier #+# #+# */
|
||||||
|
/* Updated: 2025/02/16 19:03:36 by thrieg ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
size_t i_drep_len(int n)
|
||||||
|
{
|
||||||
|
size_t len;
|
||||||
|
|
||||||
|
len = 0;
|
||||||
|
if (n < 0)
|
||||||
|
len++;
|
||||||
|
while (1)
|
||||||
|
{
|
||||||
|
len++;
|
||||||
|
n /= 10;
|
||||||
|
if (n == 0)
|
||||||
|
break ;
|
||||||
|
}
|
||||||
|
return (len);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Reimplementation of libc atoi() but sets `errno` to `ERANGE` if an
|
||||||
|
* domain error is encountered (overflow, underflow) or `EINVAL` if
|
||||||
|
* no digits characters or invalid characters at end).
|
||||||
|
*/
|
||||||
|
int ft_atoie(const char *nptr)
|
||||||
|
{
|
||||||
|
int n;
|
||||||
|
int mult;
|
||||||
|
|
||||||
|
n = 0;
|
||||||
|
mult = -1;
|
||||||
|
while (ft_isspace(*nptr))
|
||||||
|
nptr++;
|
||||||
|
if (*nptr == '+' || *nptr == '-')
|
||||||
|
{
|
||||||
|
if (*nptr == '-')
|
||||||
|
mult = 1;
|
||||||
|
nptr++;
|
||||||
|
}
|
||||||
|
if (!ft_isdigit(*nptr))
|
||||||
|
errno = EINVAL;
|
||||||
|
while (ft_isdigit(*nptr))
|
||||||
|
{
|
||||||
|
n = safe_int_sub(safe_int_mul(n, 10), *nptr - '0');
|
||||||
|
nptr++;
|
||||||
|
}
|
||||||
|
if (*nptr != '\0')
|
||||||
|
errno = EINVAL;
|
||||||
|
return (safe_int_mul(n, mult));
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned int ft_max_uint(size_t len, unsigned int a[len])
|
||||||
|
{
|
||||||
|
size_t i;
|
||||||
|
unsigned int max;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
max = 0;
|
||||||
|
while (i < len)
|
||||||
|
{
|
||||||
|
if (a[i] > max)
|
||||||
|
max = a[i];
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (max);
|
||||||
|
}
|
||||||
BIN
libft/ft_int_utils.o
Normal file
BIN
libft/ft_int_utils.o
Normal file
Binary file not shown.
20
libft/ft_isalnum.c
Executable file
20
libft/ft_isalnum.c
Executable file
|
|
@ -0,0 +1,20 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_isalnum.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/10/14 12:44:53 by thrieg #+# #+# */
|
||||||
|
/* Updated: 2025/02/16 19:03:40 by thrieg ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
int ft_isalnum(int c)
|
||||||
|
{
|
||||||
|
if (ft_isalpha(c) | ft_isdigit(c))
|
||||||
|
return (1);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
BIN
libft/ft_isalnum.o
Normal file
BIN
libft/ft_isalnum.o
Normal file
Binary file not shown.
39
libft/ft_isalpha.c
Executable file
39
libft/ft_isalpha.c
Executable file
|
|
@ -0,0 +1,39 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_isalpha.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/09/06 12:44:45 by thrieg #+# #+# */
|
||||||
|
/* Updated: 2025/02/16 19:03:42 by thrieg ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
int ft_isalpha(int c)
|
||||||
|
{
|
||||||
|
if (c < 65 || c > 122 || (c > 90 && c < 97))
|
||||||
|
return (0);
|
||||||
|
else
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
int ft_isspace(char c)
|
||||||
|
{
|
||||||
|
return (c == ' ' || c == '\f' || c == '\n' || c == '\r'
|
||||||
|
|| c == '\t' || c == '\v');
|
||||||
|
}
|
||||||
|
|
||||||
|
int ft_tolower(int c)
|
||||||
|
{
|
||||||
|
if (c >= 'A' && c <= 'Z')
|
||||||
|
return (c + ('a' - 'A'));
|
||||||
|
return (c);
|
||||||
|
}
|
||||||
|
|
||||||
|
int ft_toupper(int c)
|
||||||
|
{
|
||||||
|
if (c >= 'a' && c <= 'z')
|
||||||
|
return (c - ('a' - 'A'));
|
||||||
|
return (c);
|
||||||
|
}
|
||||||
BIN
libft/ft_isalpha.o
Normal file
BIN
libft/ft_isalpha.o
Normal file
Binary file not shown.
18
libft/ft_isascii.c
Executable file
18
libft/ft_isascii.c
Executable file
|
|
@ -0,0 +1,18 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_isascii.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/10/14 15:10:04 by thrieg #+# #+# */
|
||||||
|
/* Updated: 2025/02/16 19:03:43 by thrieg ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
int ft_isascii(int c)
|
||||||
|
{
|
||||||
|
if (c >= 0 && c < 128)
|
||||||
|
return (1);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
BIN
libft/ft_isascii.o
Normal file
BIN
libft/ft_isascii.o
Normal file
Binary file not shown.
18
libft/ft_isdigit.c
Executable file
18
libft/ft_isdigit.c
Executable file
|
|
@ -0,0 +1,18 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_isdigit.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/09/08 12:57:11 by thrieg #+# #+# */
|
||||||
|
/* Updated: 2025/02/16 19:03:45 by thrieg ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
int ft_isdigit(int c)
|
||||||
|
{
|
||||||
|
if (c < '0' || c > '9')
|
||||||
|
return (0);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
BIN
libft/ft_isdigit.o
Normal file
BIN
libft/ft_isdigit.o
Normal file
Binary file not shown.
18
libft/ft_isprint.c
Executable file
18
libft/ft_isprint.c
Executable file
|
|
@ -0,0 +1,18 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_isprint.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/10/14 15:14:35 by thrieg #+# #+# */
|
||||||
|
/* Updated: 2025/02/16 19:03:47 by thrieg ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
int ft_isprint(int c)
|
||||||
|
{
|
||||||
|
if (c < 32 || c > 126)
|
||||||
|
return (0);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
BIN
libft/ft_isprint.o
Normal file
BIN
libft/ft_isprint.o
Normal file
Binary file not shown.
83
libft/ft_itoa.c
Executable file
83
libft/ft_itoa.c
Executable file
|
|
@ -0,0 +1,83 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_itoa.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/10/15 15:29:15 by thrieg #+# #+# */
|
||||||
|
/* Updated: 2025/02/16 19:03:49 by thrieg ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
static void ft_swap(char *a, char *b)
|
||||||
|
{
|
||||||
|
char c;
|
||||||
|
|
||||||
|
c = *a;
|
||||||
|
*a = *b;
|
||||||
|
*b = c;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ft_reverse_buff(char buff[11], size_t(len))
|
||||||
|
{
|
||||||
|
size_t index;
|
||||||
|
|
||||||
|
if (buff[0] == '-')
|
||||||
|
{
|
||||||
|
index = 1;
|
||||||
|
while ((len / 2) >= index)
|
||||||
|
{
|
||||||
|
ft_swap(&buff[index], &buff[len - index]);
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
index = 0;
|
||||||
|
while (((len - 1) / 2) >= index)
|
||||||
|
{
|
||||||
|
ft_swap(&buff[index], &buff[len - index - 1]);
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static char *subfunction(char *ret, char buff[11], size_t buff_len)
|
||||||
|
{
|
||||||
|
ft_reverse_buff(buff, buff_len);
|
||||||
|
ft_memcpy(ret, (const void *)buff, buff_len);
|
||||||
|
ret[buff_len] = '\0';
|
||||||
|
return (ret);
|
||||||
|
}
|
||||||
|
|
||||||
|
//only works if an int is 32 bits, I'll maybe change it later for the bonuses
|
||||||
|
char *ft_itoa(int n)
|
||||||
|
{
|
||||||
|
char *ret;
|
||||||
|
char buff[11];
|
||||||
|
size_t buff_len;
|
||||||
|
|
||||||
|
if (n <= -2147483648)
|
||||||
|
return (ft_strdup("-2147483648"));
|
||||||
|
if (n == 0)
|
||||||
|
return (ft_strdup("0"));
|
||||||
|
buff_len = 0;
|
||||||
|
if (n < 0)
|
||||||
|
{
|
||||||
|
buff[0] = '-';
|
||||||
|
buff_len++;
|
||||||
|
n *= -1;
|
||||||
|
}
|
||||||
|
while (n > 0)
|
||||||
|
{
|
||||||
|
buff[buff_len++] = (n % 10) + '0';
|
||||||
|
n /= 10;
|
||||||
|
}
|
||||||
|
ret = malloc(sizeof(char) * (buff_len) + 1);
|
||||||
|
if (!ret)
|
||||||
|
return (NULL);
|
||||||
|
return (subfunction(ret, buff, buff_len));
|
||||||
|
}
|
||||||
BIN
libft/ft_itoa.o
Normal file
BIN
libft/ft_itoa.o
Normal file
Binary file not shown.
120
libft/ft_itoa_base.c
Executable file
120
libft/ft_itoa_base.c
Executable file
|
|
@ -0,0 +1,120 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_itoa_base.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: thrieg < thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/09/11 11:50:53 by thrieg #+# #+# */
|
||||||
|
/* Updated: 2025/11/17 17:14:48 by thrieg ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
#include "ft_vector.h"
|
||||||
|
|
||||||
|
static int ft_is_sign(char c)
|
||||||
|
{
|
||||||
|
if (c == '-' || c == '+')
|
||||||
|
return (1);
|
||||||
|
else
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
//converts nbr into a known to be valid base, then append it to vec
|
||||||
|
//returns 0 if the concatenation fails, or 1 if everything goes well
|
||||||
|
static int ft_put_ok_base(
|
||||||
|
unsigned int nbr,
|
||||||
|
char *base,
|
||||||
|
size_t base_lenght,
|
||||||
|
t_vector *vec)
|
||||||
|
{
|
||||||
|
char buffer[32];
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 31;
|
||||||
|
if (nbr == 0)
|
||||||
|
if (!ft_vector_pushback(vec, base[0]))
|
||||||
|
return (0);
|
||||||
|
while (nbr > 0)
|
||||||
|
{
|
||||||
|
buffer[i] = base[nbr % base_lenght];
|
||||||
|
nbr /= base_lenght;
|
||||||
|
i--;
|
||||||
|
}
|
||||||
|
if (!ft_vector_concat(vec, (buffer + i + 1), (31 - i)))
|
||||||
|
return (0);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
//converts nbr into an unsigned int,
|
||||||
|
//append a minus sign in vec if nbr is negative
|
||||||
|
//sets flag_pushback_failed to 1 if ft_vector_pushback returns NULL
|
||||||
|
static unsigned int ft_transform_int_to_unsigned(
|
||||||
|
int nbr,
|
||||||
|
t_vector *vec,
|
||||||
|
int *flag_pushback_failed)
|
||||||
|
{
|
||||||
|
if (nbr < 0)
|
||||||
|
{
|
||||||
|
if (!ft_vector_pushback(vec, '-'))
|
||||||
|
*flag_pushback_failed = 1;
|
||||||
|
if (nbr == -2147483648)
|
||||||
|
return (2147483648);
|
||||||
|
else
|
||||||
|
return ((unsigned int) -nbr);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
return ((unsigned int) nbr);
|
||||||
|
}
|
||||||
|
|
||||||
|
//returns 1 if the base is valid, and 0 if it's not
|
||||||
|
static int ft_is_ok_base(char *base)
|
||||||
|
{
|
||||||
|
char used_characters[128];
|
||||||
|
size_t i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
ft_bzero(used_characters, 128);
|
||||||
|
while (base[i] != '\0')
|
||||||
|
{
|
||||||
|
if (used_characters[(int) base[i]] == 0)
|
||||||
|
used_characters[(int) base[i]]++;
|
||||||
|
else
|
||||||
|
return (0);
|
||||||
|
if (ft_is_sign(base[i]))
|
||||||
|
return (0);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
if (i < 2)
|
||||||
|
return (0);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
//returns a standard C string of nbr converted in the "base" base
|
||||||
|
//base have to only cointain ascii characters
|
||||||
|
//returns NULL if the base is invalid or if an allocation failed
|
||||||
|
char *ft_itoa_base(int nbr, char *base)
|
||||||
|
{
|
||||||
|
unsigned int unsigned_nbr;
|
||||||
|
t_vector *ret;
|
||||||
|
char *ret_str;
|
||||||
|
int flag;
|
||||||
|
|
||||||
|
ret = ft_create_vector(2);
|
||||||
|
if (!ret)
|
||||||
|
return (NULL);
|
||||||
|
flag = 0;
|
||||||
|
if (!ft_is_ok_base(base))
|
||||||
|
return (ft_free_vector(&ret), NULL);
|
||||||
|
unsigned_nbr = ft_transform_int_to_unsigned(nbr, ret, &flag);
|
||||||
|
if (flag)
|
||||||
|
return (ft_free_vector(&ret), NULL);
|
||||||
|
if (!ft_put_ok_base(unsigned_nbr, base, ft_strlen(base), ret))
|
||||||
|
return (ft_free_vector(&ret), NULL);
|
||||||
|
ret_str = ft_vtoc(ret);
|
||||||
|
ft_free_vector(&ret);
|
||||||
|
if (!ret_str)
|
||||||
|
return (NULL);
|
||||||
|
return (ret_str);
|
||||||
|
}
|
||||||
BIN
libft/ft_itoa_base.o
Normal file
BIN
libft/ft_itoa_base.o
Normal file
Binary file not shown.
46
libft/ft_long_utils.c
Normal file
46
libft/ft_long_utils.c
Normal file
|
|
@ -0,0 +1,46 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_long_utils.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/10/30 10:11:17 by alier #+# #+# */
|
||||||
|
/* Updated: 2025/02/16 19:03:51 by thrieg ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include <errno.h>
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Reimplementation of libc atol() but sets `errno` to `ERANGE` if an
|
||||||
|
* domain error is encountered (overflow, underflow) or `EINVAL` if
|
||||||
|
* no digits characters or invalid characters at end).
|
||||||
|
*/
|
||||||
|
long ft_atole(const char *nptr)
|
||||||
|
{
|
||||||
|
long n;
|
||||||
|
long mult;
|
||||||
|
|
||||||
|
n = 0;
|
||||||
|
mult = -1;
|
||||||
|
while (ft_isspace(*nptr))
|
||||||
|
nptr++;
|
||||||
|
if (*nptr == '+' || *nptr == '-')
|
||||||
|
{
|
||||||
|
if (*nptr == '-')
|
||||||
|
mult = 1;
|
||||||
|
nptr++;
|
||||||
|
}
|
||||||
|
if (!ft_isdigit(*nptr))
|
||||||
|
errno = EINVAL;
|
||||||
|
while (ft_isdigit(*nptr))
|
||||||
|
{
|
||||||
|
n = safe_long_sub(safe_long_mul(n, 10), *nptr - '0');
|
||||||
|
nptr++;
|
||||||
|
}
|
||||||
|
if (*nptr != '\0')
|
||||||
|
errno = EINVAL;
|
||||||
|
return (safe_long_mul(n, mult));
|
||||||
|
}
|
||||||
BIN
libft/ft_long_utils.o
Normal file
BIN
libft/ft_long_utils.o
Normal file
Binary file not shown.
26
libft/ft_lstadd_back_bonus.c
Executable file
26
libft/ft_lstadd_back_bonus.c
Executable file
|
|
@ -0,0 +1,26 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_lstadd_back_bonus.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/10/16 12:41:51 by thrieg #+# #+# */
|
||||||
|
/* Updated: 2025/02/16 19:03:52 by thrieg ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
//returns without doing anything if lst or new is NULL, this is dumb because we
|
||||||
|
//can't report the error given the prototyping but I'm not taking any risk for
|
||||||
|
//my 3th retry...
|
||||||
|
void ft_lstadd_back(t_list **lst, t_list *new)
|
||||||
|
{
|
||||||
|
if (!lst || !new)
|
||||||
|
return ;
|
||||||
|
if (*lst == NULL)
|
||||||
|
*lst = new;
|
||||||
|
else
|
||||||
|
ft_lstlast(*lst)->next = new;
|
||||||
|
}
|
||||||
BIN
libft/ft_lstadd_back_bonus.o
Normal file
BIN
libft/ft_lstadd_back_bonus.o
Normal file
Binary file not shown.
24
libft/ft_lstadd_front_bonus.c
Executable file
24
libft/ft_lstadd_front_bonus.c
Executable file
|
|
@ -0,0 +1,24 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_lstadd_front_bonus.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/10/16 12:04:47 by thrieg #+# #+# */
|
||||||
|
/* Updated: 2025/02/16 19:03:53 by thrieg ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
//returns without doing anything if lst or new are NULL, this is dumb because we
|
||||||
|
//can't report the error given the prototyping but I'm not taking any risk for
|
||||||
|
//my 3th retry...
|
||||||
|
void ft_lstadd_front(t_list **lst, t_list *new)
|
||||||
|
{
|
||||||
|
if (!new || !lst)
|
||||||
|
return ;
|
||||||
|
new->next = *lst;
|
||||||
|
*lst = new;
|
||||||
|
}
|
||||||
BIN
libft/ft_lstadd_front_bonus.o
Normal file
BIN
libft/ft_lstadd_front_bonus.o
Normal file
Binary file not shown.
31
libft/ft_lstclear_bonus.c
Executable file
31
libft/ft_lstclear_bonus.c
Executable file
|
|
@ -0,0 +1,31 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_lstclear_bonus.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/10/16 13:00:50 by thrieg #+# #+# */
|
||||||
|
/* Updated: 2025/02/16 19:03:54 by thrieg ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
//returns without doing anything if lst or del are NULL, this is dumb because we
|
||||||
|
//can't report the error given the prototyping but I'm not taking any risk for
|
||||||
|
//my 3th retry...
|
||||||
|
void ft_lstclear(t_list **lst, void (*del)(void *))
|
||||||
|
{
|
||||||
|
t_list *next;
|
||||||
|
|
||||||
|
if (!lst || !del)
|
||||||
|
return ;
|
||||||
|
while (*lst)
|
||||||
|
{
|
||||||
|
next = (*lst)->next;
|
||||||
|
del((*lst)->content);
|
||||||
|
free(*lst);
|
||||||
|
*lst = next;
|
||||||
|
}
|
||||||
|
}
|
||||||
BIN
libft/ft_lstclear_bonus.o
Normal file
BIN
libft/ft_lstclear_bonus.o
Normal file
Binary file not shown.
24
libft/ft_lstdelone_bonus.c
Executable file
24
libft/ft_lstdelone_bonus.c
Executable file
|
|
@ -0,0 +1,24 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_lstdelone_bonus.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/10/16 12:53:23 by thrieg #+# #+# */
|
||||||
|
/* Updated: 2025/02/16 19:03:55 by thrieg ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
//returns without doing anything if lst or del are NULL, this is dumb because we
|
||||||
|
//can't report the error given the prototyping but I'm not taking any risk for
|
||||||
|
//my 3th retry...
|
||||||
|
void ft_lstdelone(t_list *lst, void (*del)(void *))
|
||||||
|
{
|
||||||
|
if (!lst || !del)
|
||||||
|
return ;
|
||||||
|
del(lst->content);
|
||||||
|
free(lst);
|
||||||
|
}
|
||||||
BIN
libft/ft_lstdelone_bonus.o
Normal file
BIN
libft/ft_lstdelone_bonus.o
Normal file
Binary file not shown.
28
libft/ft_lstiter_bonus.c
Executable file
28
libft/ft_lstiter_bonus.c
Executable file
|
|
@ -0,0 +1,28 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_lstiter_bonus.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/10/16 13:11:26 by thrieg #+# #+# */
|
||||||
|
/* Updated: 2025/02/16 19:03:57 by thrieg ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
//returns without doing anything if lst or f are NULL, this is dumb because we
|
||||||
|
//can't report the error given the prototyping but I'm not taking any risk for
|
||||||
|
//my 3th retry...
|
||||||
|
void ft_lstiter(t_list *lst, void (*f)(void *))
|
||||||
|
{
|
||||||
|
if (!lst || !f)
|
||||||
|
return ;
|
||||||
|
while (lst->next)
|
||||||
|
{
|
||||||
|
f(lst->content);
|
||||||
|
lst = lst->next;
|
||||||
|
}
|
||||||
|
f(lst->content);
|
||||||
|
}
|
||||||
BIN
libft/ft_lstiter_bonus.o
Normal file
BIN
libft/ft_lstiter_bonus.o
Normal file
Binary file not shown.
44
libft/ft_lstlast_bonus.c
Executable file
44
libft/ft_lstlast_bonus.c
Executable file
|
|
@ -0,0 +1,44 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_lstlast_bonus.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/10/16 12:21:13 by thrieg #+# #+# */
|
||||||
|
/* Updated: 2025/02/16 19:04:00 by thrieg ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
//returns NULL if lst is NULL
|
||||||
|
t_list *ft_lstlast(t_list *lst)
|
||||||
|
{
|
||||||
|
if (!lst)
|
||||||
|
return (NULL);
|
||||||
|
while (lst->next)
|
||||||
|
lst = lst->next;
|
||||||
|
return (lst);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* @return the element of `lst` at position `i`, NULL if doesn't exist.
|
||||||
|
* `ft_lstat(lst, 0)` returns `lst`.
|
||||||
|
*/
|
||||||
|
t_list *ft_lstat(t_list *lst, size_t i)
|
||||||
|
{
|
||||||
|
size_t j;
|
||||||
|
|
||||||
|
if (lst == NULL)
|
||||||
|
return (lst);
|
||||||
|
j = 0;
|
||||||
|
while (lst->next != NULL && j < i)
|
||||||
|
{
|
||||||
|
lst = lst->next;
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
if (j != i)
|
||||||
|
return (NULL);
|
||||||
|
return (lst);
|
||||||
|
}
|
||||||
BIN
libft/ft_lstlast_bonus.o
Normal file
BIN
libft/ft_lstlast_bonus.o
Normal file
Binary file not shown.
63
libft/ft_lstmap_bonus.c
Executable file
63
libft/ft_lstmap_bonus.c
Executable file
|
|
@ -0,0 +1,63 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_lstmap_bonus.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/10/16 13:22:26 by thrieg #+# #+# */
|
||||||
|
/* Updated: 2025/02/16 19:04:01 by thrieg ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
static t_list *ft_safe_create(
|
||||||
|
void *(*f)(void *),
|
||||||
|
void (*del)(void *),
|
||||||
|
void *content)
|
||||||
|
{
|
||||||
|
void *temp;
|
||||||
|
t_list *ret;
|
||||||
|
|
||||||
|
temp = f(content);
|
||||||
|
if (!temp)
|
||||||
|
return (NULL);
|
||||||
|
ret = ft_lstnew(temp);
|
||||||
|
if (!ret)
|
||||||
|
{
|
||||||
|
del(temp);
|
||||||
|
return (NULL);
|
||||||
|
}
|
||||||
|
return (ret);
|
||||||
|
}
|
||||||
|
|
||||||
|
//returns NULL without doing anything else if any argument is NULL
|
||||||
|
//returns NULL if any allocation fails
|
||||||
|
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *))
|
||||||
|
{
|
||||||
|
t_list *new_list;
|
||||||
|
t_list *new_node;
|
||||||
|
t_list *temp;
|
||||||
|
|
||||||
|
if (!lst || !f || !del)
|
||||||
|
return (NULL);
|
||||||
|
new_node = ft_safe_create(f, del, lst->content);
|
||||||
|
if (!new_node)
|
||||||
|
return (NULL);
|
||||||
|
new_list = new_node;
|
||||||
|
lst = lst->next;
|
||||||
|
while (lst)
|
||||||
|
{
|
||||||
|
temp = ft_safe_create(f, del, lst->content);
|
||||||
|
if (!temp)
|
||||||
|
{
|
||||||
|
ft_lstclear(&new_list, del);
|
||||||
|
return (NULL);
|
||||||
|
}
|
||||||
|
new_node->next = temp;
|
||||||
|
new_node = new_node->next;
|
||||||
|
lst = lst->next;
|
||||||
|
}
|
||||||
|
return (new_list);
|
||||||
|
}
|
||||||
BIN
libft/ft_lstmap_bonus.o
Normal file
BIN
libft/ft_lstmap_bonus.o
Normal file
Binary file not shown.
26
libft/ft_lstnew_bonus.c
Executable file
26
libft/ft_lstnew_bonus.c
Executable file
|
|
@ -0,0 +1,26 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_lstnew_bonus.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/10/16 11:54:17 by thrieg #+# #+# */
|
||||||
|
/* Updated: 2025/02/16 19:04:02 by thrieg ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
//returns NULL if any allocation fails
|
||||||
|
t_list *ft_lstnew(void *content)
|
||||||
|
{
|
||||||
|
t_list *ret;
|
||||||
|
|
||||||
|
ret = malloc(sizeof(t_list));
|
||||||
|
if (!ret)
|
||||||
|
return (NULL);
|
||||||
|
ret->content = content;
|
||||||
|
ret->next = NULL;
|
||||||
|
return (ret);
|
||||||
|
}
|
||||||
BIN
libft/ft_lstnew_bonus.o
Normal file
BIN
libft/ft_lstnew_bonus.o
Normal file
Binary file not shown.
29
libft/ft_lstsize_bonus.c
Executable file
29
libft/ft_lstsize_bonus.c
Executable file
|
|
@ -0,0 +1,29 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_lstsize_bonus.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/10/16 12:13:44 by thrieg #+# #+# */
|
||||||
|
/* Updated: 2025/02/16 19:04:03 by thrieg ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
//returns 0 if lst is NULL
|
||||||
|
int ft_lstsize(t_list *lst)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
if (!lst)
|
||||||
|
return (0);
|
||||||
|
i = 1;
|
||||||
|
while (lst->next)
|
||||||
|
{
|
||||||
|
i++;
|
||||||
|
lst = lst->next;
|
||||||
|
}
|
||||||
|
return (i);
|
||||||
|
}
|
||||||
BIN
libft/ft_lstsize_bonus.o
Normal file
BIN
libft/ft_lstsize_bonus.o
Normal file
Binary file not shown.
27
libft/ft_memchr.c
Executable file
27
libft/ft_memchr.c
Executable file
|
|
@ -0,0 +1,27 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_memchr.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/10/14 17:19:34 by thrieg #+# #+# */
|
||||||
|
/* Updated: 2025/02/16 19:04:05 by thrieg ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
void *ft_memchr(const void *s, int c, size_t n)
|
||||||
|
{
|
||||||
|
const char *str;
|
||||||
|
|
||||||
|
str = (const char *)s;
|
||||||
|
while (n--)
|
||||||
|
{
|
||||||
|
if (*str == (char)c)
|
||||||
|
return ((void *)str);
|
||||||
|
str++;
|
||||||
|
}
|
||||||
|
return (NULL);
|
||||||
|
}
|
||||||
BIN
libft/ft_memchr.o
Normal file
BIN
libft/ft_memchr.o
Normal file
Binary file not shown.
31
libft/ft_memcmp.c
Executable file
31
libft/ft_memcmp.c
Executable file
|
|
@ -0,0 +1,31 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_memcmp.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/10/14 17:39:59 by thrieg #+# #+# */
|
||||||
|
/* Updated: 2025/02/16 19:04:06 by thrieg ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
int ft_memcmp(const void *s1, const void *s2, size_t n)
|
||||||
|
{
|
||||||
|
const unsigned char *st1;
|
||||||
|
const unsigned char *st2;
|
||||||
|
|
||||||
|
st1 = (const unsigned char *)s1;
|
||||||
|
st2 = (const unsigned char *)s2;
|
||||||
|
while (n && (*st1 == *st2))
|
||||||
|
{
|
||||||
|
st1++;
|
||||||
|
st2++;
|
||||||
|
n--;
|
||||||
|
}
|
||||||
|
if (n == 0)
|
||||||
|
return (0);
|
||||||
|
return (*st1 - *st2);
|
||||||
|
}
|
||||||
BIN
libft/ft_memcmp.o
Normal file
BIN
libft/ft_memcmp.o
Normal file
Binary file not shown.
37
libft/ft_memcpy.c
Executable file
37
libft/ft_memcpy.c
Executable file
|
|
@ -0,0 +1,37 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_memcpy.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/10/14 15:52:40 by thrieg #+# #+# */
|
||||||
|
/* Updated: 2025/02/16 19:04:07 by thrieg ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
void *ft_memcpy(void *dest, const void *src, size_t n)
|
||||||
|
{
|
||||||
|
char *str;
|
||||||
|
unsigned long long *str_packed;
|
||||||
|
char *str_src;
|
||||||
|
unsigned long long *str_src_packed;
|
||||||
|
|
||||||
|
str_packed = (unsigned long long *)dest;
|
||||||
|
str_src_packed = (unsigned long long *)src;
|
||||||
|
while (n >= sizeof(unsigned long long))
|
||||||
|
{
|
||||||
|
*str_packed++ = *str_src_packed++;
|
||||||
|
n -= sizeof(unsigned long long);
|
||||||
|
}
|
||||||
|
str = (char *)str_packed;
|
||||||
|
str_src = (char *)str_src_packed;
|
||||||
|
while (n > 0)
|
||||||
|
{
|
||||||
|
*str++ = *str_src++;
|
||||||
|
n--;
|
||||||
|
}
|
||||||
|
return (dest);
|
||||||
|
}
|
||||||
BIN
libft/ft_memcpy.o
Normal file
BIN
libft/ft_memcpy.o
Normal file
Binary file not shown.
61
libft/ft_memmove.c
Executable file
61
libft/ft_memmove.c
Executable file
|
|
@ -0,0 +1,61 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_memmove.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/10/14 16:00:16 by thrieg #+# #+# */
|
||||||
|
/* Updated: 2025/02/16 19:04:08 by thrieg ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
static void *ft_optimised_revcpy(void *dest, const void *src, size_t n)
|
||||||
|
{
|
||||||
|
char *str;
|
||||||
|
unsigned long long *str_packed;
|
||||||
|
char *str_src;
|
||||||
|
unsigned long long *str_src_packed;
|
||||||
|
|
||||||
|
str = (char *)dest;
|
||||||
|
str_src = (char *)src;
|
||||||
|
while (n % sizeof(unsigned long long))
|
||||||
|
{
|
||||||
|
n--;
|
||||||
|
str[n] = str_src[n];
|
||||||
|
}
|
||||||
|
n /= sizeof(unsigned long long);
|
||||||
|
str_packed = (unsigned long long *)dest;
|
||||||
|
str_src_packed = (unsigned long long *)src;
|
||||||
|
while (n > 0)
|
||||||
|
{
|
||||||
|
n--;
|
||||||
|
str_packed[n] = str_src_packed[n];
|
||||||
|
}
|
||||||
|
return (dest);
|
||||||
|
}
|
||||||
|
|
||||||
|
void *ft_memmove(void *dest, const void *src, size_t n)
|
||||||
|
{
|
||||||
|
char *str;
|
||||||
|
const char *str_src;
|
||||||
|
|
||||||
|
if (!dest && !src)
|
||||||
|
return (NULL);
|
||||||
|
str = (char *)dest;
|
||||||
|
str_src = (const char *)src;
|
||||||
|
if (str > str_src)
|
||||||
|
{
|
||||||
|
if ((str + sizeof(unsigned long long)) > str_src)
|
||||||
|
return (ft_optimised_revcpy(dest, src, n));
|
||||||
|
while (n-- > 0)
|
||||||
|
str[n] = str_src[n];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ft_memcpy(dest, src, n);
|
||||||
|
}
|
||||||
|
return (dest);
|
||||||
|
}
|
||||||
BIN
libft/ft_memmove.o
Normal file
BIN
libft/ft_memmove.o
Normal file
Binary file not shown.
42
libft/ft_memset.c
Executable file
42
libft/ft_memset.c
Executable file
|
|
@ -0,0 +1,42 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_memset.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/10/14 15:40:57 by thrieg #+# #+# */
|
||||||
|
/* Updated: 2025/02/16 19:04:13 by thrieg ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
void *ft_memset(void *s, int c, size_t n)
|
||||||
|
{
|
||||||
|
char *str;
|
||||||
|
unsigned long long *str_packed;
|
||||||
|
unsigned long long cccccccc;
|
||||||
|
size_t i;
|
||||||
|
|
||||||
|
str = (char *)s;
|
||||||
|
while (n % sizeof(unsigned long long))
|
||||||
|
{
|
||||||
|
*str++ = (unsigned char)c;
|
||||||
|
n--;
|
||||||
|
}
|
||||||
|
str_packed = (unsigned long long *)str;
|
||||||
|
cccccccc = 0;
|
||||||
|
i = 0;
|
||||||
|
while (i < sizeof(unsigned long long))
|
||||||
|
{
|
||||||
|
cccccccc |= ((unsigned long long)(unsigned char)c) << (i * 8);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
while (n >= sizeof(unsigned long long))
|
||||||
|
{
|
||||||
|
*str_packed++ = cccccccc;
|
||||||
|
n -= sizeof(unsigned long long);
|
||||||
|
}
|
||||||
|
return (s);
|
||||||
|
}
|
||||||
BIN
libft/ft_memset.o
Normal file
BIN
libft/ft_memset.o
Normal file
Binary file not shown.
38
libft/ft_power.c
Normal file
38
libft/ft_power.c
Normal file
|
|
@ -0,0 +1,38 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_power.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2025/01/07 14:57:23 by thrieg #+# #+# */
|
||||||
|
/* Updated: 2025/02/16 19:04:15 by thrieg ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
ssize_t ft_power(ssize_t nb, ssize_t power)
|
||||||
|
{
|
||||||
|
ssize_t answer;
|
||||||
|
ssize_t i;
|
||||||
|
|
||||||
|
if (power < 0)
|
||||||
|
return (0);
|
||||||
|
if (power == 0)
|
||||||
|
return (1);
|
||||||
|
if (power % 2 == 0)
|
||||||
|
return (ft_power(nb * nb, power / 2));
|
||||||
|
answer = nb;
|
||||||
|
i = 1;
|
||||||
|
while (i < power)
|
||||||
|
{
|
||||||
|
if ((power - i) % 2 == 0)
|
||||||
|
{
|
||||||
|
return (answer * ft_power(nb, (power - i)));
|
||||||
|
}
|
||||||
|
answer *= nb;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (answer);
|
||||||
|
}
|
||||||
BIN
libft/ft_power.o
Normal file
BIN
libft/ft_power.o
Normal file
Binary file not shown.
104
libft/ft_printf/ft_arglist_bonus.c
Executable file
104
libft/ft_printf/ft_arglist_bonus.c
Executable file
|
|
@ -0,0 +1,104 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_arglist_bonus.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/10/27 17:04:47 by thrieg #+# #+# */
|
||||||
|
/* Updated: 2025/02/16 19:07:39 by thrieg ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "../libft.h"
|
||||||
|
#include "ft_printf.h"
|
||||||
|
#include "ft_printf_bonus.h"
|
||||||
|
|
||||||
|
static void ft_set_default_flag_values(t_arglist *arglist)
|
||||||
|
{
|
||||||
|
arglist->width_mini = 0;
|
||||||
|
arglist->precision = -1;
|
||||||
|
arglist->flag_hashtag = 0;
|
||||||
|
arglist->flag_zero = 0;
|
||||||
|
arglist->flag_justified_left = 0;
|
||||||
|
arglist->flag_space = 0;
|
||||||
|
arglist->flag_plus = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ft_handle_overriding_arguments(t_arglist *arglist)
|
||||||
|
{
|
||||||
|
if ((arglist->precision >= 0) || arglist->flag_justified_left)
|
||||||
|
arglist->flag_zero = 0;
|
||||||
|
if (arglist->flag_plus)
|
||||||
|
arglist->flag_space = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ft_handle_width_precision(
|
||||||
|
t_arglist *arglist,
|
||||||
|
int *i,
|
||||||
|
const char *str,
|
||||||
|
va_list *args)
|
||||||
|
{
|
||||||
|
if (ft_isdigit(str[*i]))
|
||||||
|
{
|
||||||
|
arglist->width_mini = ft_atoi(&str[*i], NULL);
|
||||||
|
while (ft_isdigit(str[*i]))
|
||||||
|
(*i)++;
|
||||||
|
}
|
||||||
|
else if (str[*i] == '*')
|
||||||
|
{
|
||||||
|
arglist->width_mini = va_arg(*args, int);
|
||||||
|
(*i)++;
|
||||||
|
}
|
||||||
|
if (str[*i] == '.')
|
||||||
|
{
|
||||||
|
(*i)++;
|
||||||
|
if (str[*i] == '*')
|
||||||
|
{
|
||||||
|
arglist->precision = va_arg(*args, int);
|
||||||
|
(*i)++;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
arglist->precision = ft_atoi(&str[*i], NULL);
|
||||||
|
while (ft_isdigit(str[*i]))
|
||||||
|
(*i)++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ft_handle_flags(t_arglist *arglist, int *i, const char *str)
|
||||||
|
{
|
||||||
|
while (str[*i] == '#' || str[*i] == '0' || str[*i] == '-'
|
||||||
|
|| str[*i] == ' ' || str[*i] == '+')
|
||||||
|
{
|
||||||
|
if (str[*i] == '#')
|
||||||
|
arglist->flag_hashtag = 1;
|
||||||
|
if (str[*i] == '0')
|
||||||
|
arglist->flag_zero = 1;
|
||||||
|
if (str[*i] == '-')
|
||||||
|
arglist->flag_justified_left = 1;
|
||||||
|
if (str[*i] == ' ')
|
||||||
|
arglist->flag_space = 1;
|
||||||
|
if (str[*i] == '+')
|
||||||
|
arglist->flag_plus = 1;
|
||||||
|
(*i)++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
t_arglist *ft_create_arglist(const char *str, va_list *args)
|
||||||
|
{
|
||||||
|
t_arglist *arglist;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
arglist = malloc(sizeof(t_arglist));
|
||||||
|
if (!arglist)
|
||||||
|
return (NULL);
|
||||||
|
ft_set_default_flag_values(arglist);
|
||||||
|
i = 0;
|
||||||
|
if (str[i] == '%')
|
||||||
|
i++;
|
||||||
|
ft_handle_flags(arglist, &i, str);
|
||||||
|
ft_handle_width_precision(arglist, &i, str, args);
|
||||||
|
arglist->size_of_argument_string = i;
|
||||||
|
ft_handle_overriding_arguments(arglist);
|
||||||
|
return (arglist);
|
||||||
|
}
|
||||||
BIN
libft/ft_printf/ft_arglist_bonus.o
Normal file
BIN
libft/ft_printf/ft_arglist_bonus.o
Normal file
Binary file not shown.
75
libft/ft_printf/ft_case_d_bonus.c
Executable file
75
libft/ft_printf/ft_case_d_bonus.c
Executable file
|
|
@ -0,0 +1,75 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_case_d_bonus.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/10/25 17:12:38 by thrieg #+# #+# */
|
||||||
|
/* Updated: 2025/02/16 19:07:44 by thrieg ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "../libft.h"
|
||||||
|
#include "ft_printf.h"
|
||||||
|
#include "ft_printf_bonus.h"
|
||||||
|
|
||||||
|
int padding_if_needed_before(
|
||||||
|
t_arglist *arglist,
|
||||||
|
char caller,
|
||||||
|
size_t len,
|
||||||
|
t_vector *vec);
|
||||||
|
int padding_if_needed_after(
|
||||||
|
t_arglist *arglist,
|
||||||
|
char caller,
|
||||||
|
size_t len,
|
||||||
|
t_vector *vec);
|
||||||
|
char *init_str_to_cat(t_arglist *arglist, unsigned int nbr);
|
||||||
|
void convert_precision_if_flag_zero(t_arglist *arglist, int nbr);
|
||||||
|
int ft_handle_if_precision(
|
||||||
|
t_arglist *arglist,
|
||||||
|
int nbr,
|
||||||
|
t_vector *vec,
|
||||||
|
int *size);
|
||||||
|
int ft_handle_if_not_precision(
|
||||||
|
t_arglist *arglist,
|
||||||
|
int nbr,
|
||||||
|
t_vector *vec,
|
||||||
|
int *size);
|
||||||
|
int ft_handle_concat_and_padding(
|
||||||
|
t_arglist *arglist,
|
||||||
|
char *str_to_cat,
|
||||||
|
t_vector *vec,
|
||||||
|
int size);
|
||||||
|
|
||||||
|
//flags to handle: zero, space, plus, precision, justified left... hard mode
|
||||||
|
//flag zero padding without precision behaves just like a precision
|
||||||
|
//(zeroes after the sign) and when it's spaces they're before the sign
|
||||||
|
int ft_case_d_bonus(t_vector *vec, va_list *args, t_arglist *arglist)
|
||||||
|
{
|
||||||
|
char *str_to_cat;
|
||||||
|
int nbr;
|
||||||
|
int size;
|
||||||
|
|
||||||
|
nbr = va_arg(*args, int);
|
||||||
|
str_to_cat = init_str_to_cat(arglist, nbr);
|
||||||
|
if (!str_to_cat)
|
||||||
|
return (0);
|
||||||
|
convert_precision_if_flag_zero(arglist, nbr);
|
||||||
|
size = (int)ft_strlen(str_to_cat);
|
||||||
|
if (nbr < 0)
|
||||||
|
size--;
|
||||||
|
if ((size < arglist->precision) && (arglist->precision >= 0))
|
||||||
|
{
|
||||||
|
if (!ft_handle_if_precision(arglist, nbr, vec, &size))
|
||||||
|
return (free(str_to_cat), 0);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (!ft_handle_if_not_precision(arglist, nbr, vec, &size))
|
||||||
|
return (free(str_to_cat), 0);
|
||||||
|
}
|
||||||
|
if (!ft_handle_concat_and_padding(arglist, str_to_cat, vec, size))
|
||||||
|
return (free(str_to_cat), 0);
|
||||||
|
return (free(str_to_cat), 1);
|
||||||
|
}
|
||||||
BIN
libft/ft_printf/ft_case_d_bonus.o
Normal file
BIN
libft/ft_printf/ft_case_d_bonus.o
Normal file
Binary file not shown.
115
libft/ft_printf/ft_case_d_utils_bonus.c
Executable file
115
libft/ft_printf/ft_case_d_utils_bonus.c
Executable file
|
|
@ -0,0 +1,115 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_case_d_utils_bonus.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/10/27 15:59:04 by thrieg #+# #+# */
|
||||||
|
/* Updated: 2025/02/16 19:07:45 by thrieg ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "../libft.h"
|
||||||
|
#include "ft_printf.h"
|
||||||
|
#include "ft_printf_bonus.h"
|
||||||
|
|
||||||
|
int padding_if_needed_before(
|
||||||
|
t_arglist *arglist,
|
||||||
|
char caller,
|
||||||
|
size_t len,
|
||||||
|
t_vector *vec);
|
||||||
|
int padding_if_needed_after(
|
||||||
|
t_arglist *arglist,
|
||||||
|
char caller,
|
||||||
|
size_t len,
|
||||||
|
t_vector *vec);
|
||||||
|
|
||||||
|
char *init_str_to_cat(t_arglist *arglist, unsigned int nbr)
|
||||||
|
{
|
||||||
|
if (nbr == 0 && arglist->precision == 0)
|
||||||
|
return (ft_strdup(""));
|
||||||
|
return (ft_itoa(nbr));
|
||||||
|
}
|
||||||
|
|
||||||
|
void convert_precision_if_flag_zero(t_arglist *arglist, int nbr)
|
||||||
|
{
|
||||||
|
if ((arglist->precision < 0) && !(arglist->flag_justified_left)
|
||||||
|
&& arglist->flag_zero)
|
||||||
|
{
|
||||||
|
arglist->precision = arglist->width_mini;
|
||||||
|
arglist->width_mini = 0;
|
||||||
|
if (nbr < 0 || arglist->flag_plus || arglist->flag_space)
|
||||||
|
arglist->precision--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int ft_handle_if_precision(
|
||||||
|
t_arglist *arglist,
|
||||||
|
int nbr,
|
||||||
|
t_vector *vec,
|
||||||
|
int *size)
|
||||||
|
{
|
||||||
|
if (nbr < 0 || arglist->flag_plus || arglist->flag_space)
|
||||||
|
{
|
||||||
|
if (padding_if_needed_before(arglist, 'd',
|
||||||
|
arglist->precision + 1, vec) < 0)
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
if (padding_if_needed_before(arglist, 'd', arglist->precision, vec) < 0)
|
||||||
|
return (0);
|
||||||
|
if (arglist->flag_plus && (nbr >= 0) && !ft_vector_pushback(vec, '+'))
|
||||||
|
return (0);
|
||||||
|
if (arglist->flag_space && (nbr >= 0) && !(arglist->flag_plus) && nbr >= 0)
|
||||||
|
if (!ft_vector_pushback(vec, ' '))
|
||||||
|
return (0);
|
||||||
|
if (nbr < 0 && !ft_vector_pushback(vec, '-'))
|
||||||
|
return (0);
|
||||||
|
while ((*size) < arglist->precision)
|
||||||
|
{
|
||||||
|
if (!ft_vector_pushback(vec, '0'))
|
||||||
|
return (0);
|
||||||
|
(*size)++;
|
||||||
|
}
|
||||||
|
if (nbr < 0 || arglist->flag_plus || arglist->flag_space)
|
||||||
|
(*size)++;
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
int ft_handle_if_not_precision(
|
||||||
|
t_arglist *arglist,
|
||||||
|
int nbr,
|
||||||
|
t_vector *vec,
|
||||||
|
int *size)
|
||||||
|
{
|
||||||
|
if (nbr < 0 || arglist->flag_space || arglist->flag_plus)
|
||||||
|
(*size)++;
|
||||||
|
if (padding_if_needed_before(arglist, 'd', (*size), vec) < 0)
|
||||||
|
return (0);
|
||||||
|
if (arglist->flag_plus && (nbr >= 0))
|
||||||
|
if (!ft_vector_pushback(vec, '+'))
|
||||||
|
return (0);
|
||||||
|
if (arglist->flag_space && (nbr >= 0) && !(arglist->flag_plus))
|
||||||
|
if (nbr >= 0)
|
||||||
|
if (!ft_vector_pushback(vec, ' '))
|
||||||
|
return (0);
|
||||||
|
if (nbr < 0)
|
||||||
|
if (!ft_vector_pushback(vec, '-'))
|
||||||
|
return (0);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
int ft_handle_concat_and_padding(
|
||||||
|
t_arglist *arglist,
|
||||||
|
char *str_to_cat,
|
||||||
|
t_vector *vec,
|
||||||
|
int size)
|
||||||
|
{
|
||||||
|
if (!ft_vector_concat(vec, str_to_cat + ft_strnonchr(str_to_cat, '-'),
|
||||||
|
ft_strlen(str_to_cat) - ft_strnonchr(str_to_cat, '-')))
|
||||||
|
return (0);
|
||||||
|
if (padding_if_needed_after(arglist, 'd', size, vec) < 0)
|
||||||
|
return (0);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
BIN
libft/ft_printf/ft_case_d_utils_bonus.o
Normal file
BIN
libft/ft_printf/ft_case_d_utils_bonus.o
Normal file
Binary file not shown.
115
libft/ft_printf/ft_case_lowx_bonus.c
Normal file
115
libft/ft_printf/ft_case_lowx_bonus.c
Normal file
|
|
@ -0,0 +1,115 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_case_lowx_bonus.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/10/25 17:09:37 by thrieg #+# #+# */
|
||||||
|
/* Updated: 2025/02/16 19:07:46 by thrieg ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "../libft.h"
|
||||||
|
#include "ft_printf.h"
|
||||||
|
#include "ft_printf_bonus.h"
|
||||||
|
|
||||||
|
int padding_if_needed_before(
|
||||||
|
t_arglist *arglist,
|
||||||
|
char caller,
|
||||||
|
size_t len,
|
||||||
|
t_vector *vec);
|
||||||
|
int padding_if_needed_after(
|
||||||
|
t_arglist *arglist,
|
||||||
|
char caller,
|
||||||
|
size_t len,
|
||||||
|
t_vector *vec);
|
||||||
|
|
||||||
|
static int handle_hashtag_prefix(
|
||||||
|
t_vector *vec,
|
||||||
|
t_arglist *arglist,
|
||||||
|
unsigned int nbr,
|
||||||
|
int flag_zero)
|
||||||
|
{
|
||||||
|
if (arglist->flag_hashtag && !flag_zero && nbr != 0)
|
||||||
|
{
|
||||||
|
if (!ft_vector_concat(vec, "0x", 2))
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int apply_precision_padding(t_vector *vec, size_t *size, int precision)
|
||||||
|
{
|
||||||
|
while ((int)(*size)++ < precision)
|
||||||
|
{
|
||||||
|
if (!ft_vector_pushback(vec, '0'))
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
(*size)--;
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
static char *init_str_to_cat(t_arglist *arglist, unsigned int nbr)
|
||||||
|
{
|
||||||
|
if (nbr == 0 && arglist->precision == 0)
|
||||||
|
return (ft_strdup(""));
|
||||||
|
return (ft_utoa_base(nbr, "0123456789abcdef"));
|
||||||
|
}
|
||||||
|
|
||||||
|
static int handle_padding_and_prefix(
|
||||||
|
t_vector *vec,
|
||||||
|
t_arglist *arglist,
|
||||||
|
size_t size,
|
||||||
|
unsigned int nbr)
|
||||||
|
{
|
||||||
|
if (arglist->flag_hashtag && arglist->flag_zero && nbr != 0)
|
||||||
|
{
|
||||||
|
if (!ft_vector_concat(vec, "0x", 2))
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
if (((int)size < arglist->precision) && (arglist->precision >= 0))
|
||||||
|
{
|
||||||
|
if (padding_if_needed_before(arglist, 'x', arglist->precision
|
||||||
|
+ (arglist->flag_hashtag * 2 * (nbr != 0)), vec) < 0)
|
||||||
|
return (0);
|
||||||
|
if (!handle_hashtag_prefix(vec, arglist, nbr, arglist->flag_zero))
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (arglist->flag_hashtag && nbr != 0)
|
||||||
|
size += 2;
|
||||||
|
if (padding_if_needed_before(arglist, 'x', size, vec) < 0)
|
||||||
|
return (0);
|
||||||
|
if (!handle_hashtag_prefix(vec, arglist, nbr, arglist->flag_zero))
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
int ft_case_x_bonus(t_vector *vec, va_list *args, t_arglist *arglist)
|
||||||
|
{
|
||||||
|
char *str_to_cat;
|
||||||
|
size_t size;
|
||||||
|
unsigned int nbr;
|
||||||
|
|
||||||
|
nbr = va_arg(*args, unsigned int);
|
||||||
|
str_to_cat = init_str_to_cat(arglist, nbr);
|
||||||
|
if (!str_to_cat)
|
||||||
|
return (0);
|
||||||
|
size = ft_strlen(str_to_cat);
|
||||||
|
if (!handle_padding_and_prefix(vec, arglist, size, nbr))
|
||||||
|
return (free(str_to_cat), 0);
|
||||||
|
if (((int)size < arglist->precision) && (arglist->precision >= 0))
|
||||||
|
{
|
||||||
|
if (!apply_precision_padding(vec, &size, arglist->precision))
|
||||||
|
return (free(str_to_cat), 0);
|
||||||
|
}
|
||||||
|
if (!ft_vector_concat(vec, str_to_cat, ft_strlen(str_to_cat)))
|
||||||
|
return (free(str_to_cat), 0);
|
||||||
|
free(str_to_cat);
|
||||||
|
if (padding_if_needed_after(arglist, 'x', size, vec) < 0)
|
||||||
|
return (0);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
BIN
libft/ft_printf/ft_case_lowx_bonus.o
Normal file
BIN
libft/ft_printf/ft_case_lowx_bonus.o
Normal file
Binary file not shown.
70
libft/ft_printf/ft_case_p_bonus.c
Executable file
70
libft/ft_printf/ft_case_p_bonus.c
Executable file
|
|
@ -0,0 +1,70 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_case_p_bonus.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/10/23 16:22:40 by thrieg #+# #+# */
|
||||||
|
/* Updated: 2025/02/16 19:07:47 by thrieg ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "../libft.h"
|
||||||
|
#include "ft_printf.h"
|
||||||
|
#include "ft_printf_bonus.h"
|
||||||
|
|
||||||
|
int padding_if_needed_before(
|
||||||
|
t_arglist *arglist,
|
||||||
|
char caller,
|
||||||
|
size_t len,
|
||||||
|
t_vector *vec);
|
||||||
|
int padding_if_needed_after(
|
||||||
|
t_arglist *arglist,
|
||||||
|
char caller,
|
||||||
|
size_t len,
|
||||||
|
t_vector *vec);
|
||||||
|
|
||||||
|
//zero and precision flags have undefined behavior here
|
||||||
|
int ft_case_p_bonus_utils(t_vector *vec, char *str_to_cat, t_arglist *arglist)
|
||||||
|
{
|
||||||
|
size_t index;
|
||||||
|
size_t size;
|
||||||
|
|
||||||
|
index = ft_strnonchr(str_to_cat, '0');
|
||||||
|
size = ft_strlen(str_to_cat + index);
|
||||||
|
if (padding_if_needed_before(arglist, 'p', (size + 2), vec) < 0)
|
||||||
|
return (0);
|
||||||
|
if (!ft_vector_concat(vec, "0x", 2))
|
||||||
|
return (0);
|
||||||
|
if (!ft_vector_concat(vec, (str_to_cat + index), size))
|
||||||
|
return (0);
|
||||||
|
if (padding_if_needed_after(arglist, 'p', (size + 2), vec) < 0)
|
||||||
|
return (0);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
//zero and precision flags have undefined behavior here
|
||||||
|
int ft_case_p_bonus(t_vector *vec, va_list *args, t_arglist *arglist)
|
||||||
|
{
|
||||||
|
char *str_to_cat;
|
||||||
|
void *arg;
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
arg = va_arg(*args, void *);
|
||||||
|
if (!arg)
|
||||||
|
{
|
||||||
|
if (padding_if_needed_before(arglist, 'p', 5, vec) < 0)
|
||||||
|
return (0);
|
||||||
|
if (!ft_vector_concat(vec, "(nil)", 5))
|
||||||
|
return (0);
|
||||||
|
if (padding_if_needed_after(arglist, 'p', 5, vec) < 0)
|
||||||
|
return (0);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
str_to_cat = ft_addr_to_strhex(arg);
|
||||||
|
if (!str_to_cat)
|
||||||
|
return (0);
|
||||||
|
ret = ft_case_p_bonus_utils(vec, str_to_cat, arglist);
|
||||||
|
return (free(str_to_cat), ret);
|
||||||
|
}
|
||||||
BIN
libft/ft_printf/ft_case_p_bonus.o
Normal file
BIN
libft/ft_printf/ft_case_p_bonus.o
Normal file
Binary file not shown.
64
libft/ft_printf/ft_case_s_bonus.c
Executable file
64
libft/ft_printf/ft_case_s_bonus.c
Executable file
|
|
@ -0,0 +1,64 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_case_s_bonus.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/10/25 17:03:40 by thrieg #+# #+# */
|
||||||
|
/* Updated: 2025/02/16 19:07:48 by thrieg ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "../libft.h"
|
||||||
|
#include "ft_printf.h"
|
||||||
|
#include "ft_printf_bonus.h"
|
||||||
|
|
||||||
|
int padding_if_needed_before(
|
||||||
|
t_arglist *arglist,
|
||||||
|
char caller,
|
||||||
|
size_t len,
|
||||||
|
t_vector *vec);
|
||||||
|
int padding_if_needed_after(
|
||||||
|
t_arglist *arglist,
|
||||||
|
char caller,
|
||||||
|
size_t len,
|
||||||
|
t_vector *vec);
|
||||||
|
|
||||||
|
static int ft_s_sub_bonus(t_vector *vec, char *str_to_cat, t_arglist *arglist)
|
||||||
|
{
|
||||||
|
size_t size;
|
||||||
|
|
||||||
|
if (!str_to_cat)
|
||||||
|
{
|
||||||
|
size = 6;
|
||||||
|
if ((int)size > arglist->precision && (arglist->precision >= 0))
|
||||||
|
size = 0;
|
||||||
|
if (padding_if_needed_before(arglist, 's', size, vec) < 0)
|
||||||
|
return (0);
|
||||||
|
if (!ft_vector_concat(vec, "(null)", size))
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
size = ft_strlen(str_to_cat);
|
||||||
|
if ((int)size > arglist->precision && (arglist->precision >= 0))
|
||||||
|
size = arglist->precision;
|
||||||
|
if (padding_if_needed_before(arglist, 's', size, vec) < 0)
|
||||||
|
return (0);
|
||||||
|
if (!ft_vector_concat(vec, str_to_cat, size))
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
if (padding_if_needed_after(arglist, 's', size, vec) < 0)
|
||||||
|
return (0);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
//flag 0 has undefined behavior with c so the minimum width has to be spaces
|
||||||
|
int ft_case_s_bonus(t_vector *vec, va_list *args, t_arglist *arglist)
|
||||||
|
{
|
||||||
|
char *str_to_cat;
|
||||||
|
|
||||||
|
str_to_cat = va_arg(*args, char *);
|
||||||
|
return (ft_s_sub_bonus(vec, str_to_cat, arglist));
|
||||||
|
}
|
||||||
BIN
libft/ft_printf/ft_case_s_bonus.o
Normal file
BIN
libft/ft_printf/ft_case_s_bonus.o
Normal file
Binary file not shown.
72
libft/ft_printf/ft_case_u_bonus.c
Executable file
72
libft/ft_printf/ft_case_u_bonus.c
Executable file
|
|
@ -0,0 +1,72 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_case_u_bonus.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/10/25 17:07:14 by thrieg #+# #+# */
|
||||||
|
/* Updated: 2025/02/16 19:07:49 by thrieg ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "../libft.h"
|
||||||
|
#include "ft_printf.h"
|
||||||
|
#include "ft_printf_bonus.h"
|
||||||
|
|
||||||
|
int padding_if_needed_before(
|
||||||
|
t_arglist *arglist,
|
||||||
|
char caller,
|
||||||
|
size_t len,
|
||||||
|
t_vector *vec);
|
||||||
|
int padding_if_needed_after(
|
||||||
|
t_arglist *arglist,
|
||||||
|
char caller,
|
||||||
|
size_t len,
|
||||||
|
t_vector *vec);
|
||||||
|
|
||||||
|
static int ft_handle_padding_precision_bonus(
|
||||||
|
t_vector *vec,
|
||||||
|
size_t size,
|
||||||
|
t_arglist *arglist,
|
||||||
|
int padding_length)
|
||||||
|
{
|
||||||
|
if (padding_if_needed_before(arglist, 'u', padding_length, vec) < 0)
|
||||||
|
return (0);
|
||||||
|
while ((int)size++ < arglist->precision)
|
||||||
|
{
|
||||||
|
if (!ft_vector_pushback(vec, '0'))
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
size--;
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Plus and space flags have undefined behavior here
|
||||||
|
int ft_case_u_bonus(t_vector *vec, va_list *args, t_arglist *arglist)
|
||||||
|
{
|
||||||
|
char *str_to_cat;
|
||||||
|
int size;
|
||||||
|
unsigned int nbr;
|
||||||
|
int padding_length;
|
||||||
|
|
||||||
|
nbr = va_arg(*args, unsigned int);
|
||||||
|
if (nbr == 0 && arglist->precision == 0)
|
||||||
|
str_to_cat = ft_strdup("");
|
||||||
|
else
|
||||||
|
str_to_cat = ft_utoa(nbr);
|
||||||
|
if (!str_to_cat)
|
||||||
|
return (0);
|
||||||
|
size = (int)ft_strlen(str_to_cat);
|
||||||
|
if (size < arglist->precision)
|
||||||
|
padding_length = arglist->precision;
|
||||||
|
else
|
||||||
|
padding_length = size;
|
||||||
|
if (!ft_handle_padding_precision_bonus(vec, size, arglist, padding_length))
|
||||||
|
return (free(str_to_cat), 0);
|
||||||
|
if (!ft_vector_concat(vec, str_to_cat, size))
|
||||||
|
return (free(str_to_cat), 0);
|
||||||
|
if (padding_if_needed_after(arglist, 'u', padding_length, vec) < 0)
|
||||||
|
return (free(str_to_cat), 0);
|
||||||
|
return (free(str_to_cat), 1);
|
||||||
|
}
|
||||||
BIN
libft/ft_printf/ft_case_u_bonus.o
Normal file
BIN
libft/ft_printf/ft_case_u_bonus.o
Normal file
Binary file not shown.
115
libft/ft_printf/ft_case_uppx_bonus.c
Normal file
115
libft/ft_printf/ft_case_uppx_bonus.c
Normal file
|
|
@ -0,0 +1,115 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_case_uppx_bonus.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/10/25 17:11:07 by thrieg #+# #+# */
|
||||||
|
/* Updated: 2025/02/16 19:07:50 by thrieg ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "../libft.h"
|
||||||
|
#include "ft_printf.h"
|
||||||
|
#include "ft_printf_bonus.h"
|
||||||
|
|
||||||
|
int padding_if_needed_before(
|
||||||
|
t_arglist *arglist,
|
||||||
|
char caller,
|
||||||
|
size_t len,
|
||||||
|
t_vector *vec);
|
||||||
|
int padding_if_needed_after(
|
||||||
|
t_arglist *arglist,
|
||||||
|
char caller,
|
||||||
|
size_t len,
|
||||||
|
t_vector *vec);
|
||||||
|
|
||||||
|
static int handle_hashtag_prefix(
|
||||||
|
t_vector *vec,
|
||||||
|
t_arglist *arglist,
|
||||||
|
unsigned int nbr,
|
||||||
|
int flag_zero)
|
||||||
|
{
|
||||||
|
if (arglist->flag_hashtag && !flag_zero && nbr != 0)
|
||||||
|
{
|
||||||
|
if (!ft_vector_concat(vec, "0X", 2))
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int apply_precision_padding(t_vector *vec, size_t *size, int precision)
|
||||||
|
{
|
||||||
|
while ((int)(*size)++ < precision)
|
||||||
|
{
|
||||||
|
if (!ft_vector_pushback(vec, '0'))
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
(*size)--;
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
static char *init_str_to_cat(t_arglist *arglist, unsigned int nbr)
|
||||||
|
{
|
||||||
|
if (nbr == 0 && arglist->precision == 0)
|
||||||
|
return (ft_strdup(""));
|
||||||
|
return (ft_utoa_base(nbr, "0123456789ABCDEF"));
|
||||||
|
}
|
||||||
|
|
||||||
|
static int handle_padding_and_prefix(
|
||||||
|
t_vector *vec,
|
||||||
|
t_arglist *arglist,
|
||||||
|
size_t size,
|
||||||
|
unsigned int nbr)
|
||||||
|
{
|
||||||
|
if (arglist->flag_hashtag && arglist->flag_zero && nbr != 0)
|
||||||
|
{
|
||||||
|
if (!ft_vector_concat(vec, "0X", 2))
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
if (((int)size < arglist->precision) && (arglist->precision >= 0))
|
||||||
|
{
|
||||||
|
if (padding_if_needed_before(arglist, 'X', arglist->precision
|
||||||
|
+ (arglist->flag_hashtag * 2 * (nbr != 0)), vec) < 0)
|
||||||
|
return (0);
|
||||||
|
if (!handle_hashtag_prefix(vec, arglist, nbr, arglist->flag_zero))
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (arglist->flag_hashtag && nbr != 0)
|
||||||
|
size += 2;
|
||||||
|
if (padding_if_needed_before(arglist, 'X', size, vec) < 0)
|
||||||
|
return (0);
|
||||||
|
if (!handle_hashtag_prefix(vec, arglist, nbr, arglist->flag_zero))
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
int ft_case_upperx_bonus(t_vector *vec, va_list *args, t_arglist *arglist)
|
||||||
|
{
|
||||||
|
char *str_to_cat;
|
||||||
|
size_t size;
|
||||||
|
unsigned int nbr;
|
||||||
|
|
||||||
|
nbr = va_arg(*args, unsigned int);
|
||||||
|
str_to_cat = init_str_to_cat(arglist, nbr);
|
||||||
|
if (!str_to_cat)
|
||||||
|
return (0);
|
||||||
|
size = ft_strlen(str_to_cat);
|
||||||
|
if (!handle_padding_and_prefix(vec, arglist, size, nbr))
|
||||||
|
return (free(str_to_cat), 0);
|
||||||
|
if (((int)size < arglist->precision) && (arglist->precision >= 0))
|
||||||
|
{
|
||||||
|
if (!apply_precision_padding(vec, &size, arglist->precision))
|
||||||
|
return (free(str_to_cat), 0);
|
||||||
|
}
|
||||||
|
if (!ft_vector_concat(vec, str_to_cat, ft_strlen(str_to_cat)))
|
||||||
|
return (free(str_to_cat), 0);
|
||||||
|
free(str_to_cat);
|
||||||
|
if (padding_if_needed_after(arglist, 'X', size, vec) < 0)
|
||||||
|
return (0);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
BIN
libft/ft_printf/ft_case_uppx_bonus.o
Normal file
BIN
libft/ft_printf/ft_case_uppx_bonus.o
Normal file
Binary file not shown.
52
libft/ft_printf/ft_cases_easy_bonus.c
Executable file
52
libft/ft_printf/ft_cases_easy_bonus.c
Executable file
|
|
@ -0,0 +1,52 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_cases_easy_bonus.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/10/23 13:03:05 by thrieg #+# #+# */
|
||||||
|
/* Updated: 2025/02/16 19:07:51 by thrieg ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "../libft.h"
|
||||||
|
#include "ft_printf.h"
|
||||||
|
#include "ft_printf_bonus.h"
|
||||||
|
|
||||||
|
int padding_if_needed_before(
|
||||||
|
t_arglist *arglist,
|
||||||
|
char caller,
|
||||||
|
size_t len,
|
||||||
|
t_vector *vec);
|
||||||
|
int padding_if_needed_after(
|
||||||
|
t_arglist *arglist,
|
||||||
|
char caller,
|
||||||
|
size_t len,
|
||||||
|
t_vector *vec);
|
||||||
|
|
||||||
|
//flag 0 has undefined behavior with c so the minimum width has to be spaces
|
||||||
|
int ft_case_c_bonus(t_vector *vec, va_list *args, t_arglist *arglist)
|
||||||
|
{
|
||||||
|
if (padding_if_needed_before(arglist, 'c', 1, vec) < 0)
|
||||||
|
return (0);
|
||||||
|
if (!ft_vector_pushback(vec, (char)va_arg(*args, int)))
|
||||||
|
return (0);
|
||||||
|
if (padding_if_needed_after(arglist, 'c', 1, vec) < 0)
|
||||||
|
return (0);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
int ft_case_percent_bonus(t_vector *vec)
|
||||||
|
{
|
||||||
|
if (!ft_vector_pushback(vec, '%'))
|
||||||
|
return (0);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
int ft_case_d_bonus(t_vector *vec, va_list *args, t_arglist *arglist);
|
||||||
|
|
||||||
|
int ft_case_i_bonus(t_vector *vec, va_list *args, t_arglist *arglist)
|
||||||
|
{
|
||||||
|
return (ft_case_d_bonus(vec, args, arglist));
|
||||||
|
}
|
||||||
BIN
libft/ft_printf/ft_cases_easy_bonus.o
Normal file
BIN
libft/ft_printf/ft_cases_easy_bonus.o
Normal file
Binary file not shown.
85
libft/ft_printf/ft_cases_mandatory_one.c
Executable file
85
libft/ft_printf/ft_cases_mandatory_one.c
Executable file
|
|
@ -0,0 +1,85 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_cases_mandatory_one.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/10/22 18:52:37 by thrieg #+# #+# */
|
||||||
|
/* Updated: 2025/02/16 19:07:52 by thrieg ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "../libft.h"
|
||||||
|
#include "ft_printf.h"
|
||||||
|
|
||||||
|
int ft_case_c(t_vector *vec, va_list *args)
|
||||||
|
{
|
||||||
|
if (!ft_vector_pushback(vec, (char)va_arg(*args, int)))
|
||||||
|
return (0);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
int ft_case_percent(t_vector *vec)
|
||||||
|
{
|
||||||
|
if (!ft_vector_pushback(vec, '%'))
|
||||||
|
return (0);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
int ft_case_s(t_vector *vec, va_list *args)
|
||||||
|
{
|
||||||
|
char *str_to_cat;
|
||||||
|
|
||||||
|
str_to_cat = va_arg(*args, char *);
|
||||||
|
if (!str_to_cat)
|
||||||
|
{
|
||||||
|
if (!ft_vector_concat(vec, "(null)", 6))
|
||||||
|
return (free(str_to_cat), 0);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
str_to_cat = ft_strdup(str_to_cat);
|
||||||
|
if (!ft_vector_concat(vec, str_to_cat, ft_strlen(str_to_cat)))
|
||||||
|
return (free(str_to_cat), 0);
|
||||||
|
free(str_to_cat);
|
||||||
|
}
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
int ft_case_p(t_vector *vec, va_list *args)
|
||||||
|
{
|
||||||
|
char *str_to_cat;
|
||||||
|
void *arg;
|
||||||
|
size_t index;
|
||||||
|
|
||||||
|
arg = va_arg(*args, void *);
|
||||||
|
if (!arg)
|
||||||
|
{
|
||||||
|
if (!ft_vector_concat(vec, "(nil)", 5))
|
||||||
|
return (0);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
if (!ft_vector_concat(vec, "0x", 2))
|
||||||
|
return (0);
|
||||||
|
str_to_cat = ft_addr_to_strhex(arg);
|
||||||
|
if (!str_to_cat)
|
||||||
|
return (0);
|
||||||
|
index = ft_strnonchr(str_to_cat, '0');
|
||||||
|
if (!ft_vector_concat(vec, str_to_cat + index,
|
||||||
|
ft_strlen(str_to_cat) - index))
|
||||||
|
return (free(str_to_cat), 0);
|
||||||
|
free(str_to_cat);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
int ft_case_u(t_vector *vec, va_list *args)
|
||||||
|
{
|
||||||
|
char *str_to_cat;
|
||||||
|
|
||||||
|
str_to_cat = ft_utoa(va_arg(*args, unsigned int));
|
||||||
|
if (!ft_vector_concat(vec, str_to_cat, ft_strlen(str_to_cat)))
|
||||||
|
return (free(str_to_cat), 0);
|
||||||
|
free(str_to_cat);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
BIN
libft/ft_printf/ft_cases_mandatory_one.o
Normal file
BIN
libft/ft_printf/ft_cases_mandatory_one.o
Normal file
Binary file not shown.
58
libft/ft_printf/ft_cases_mandatory_two.c
Executable file
58
libft/ft_printf/ft_cases_mandatory_two.c
Executable file
|
|
@ -0,0 +1,58 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_cases_mandatory_two.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/10/22 18:52:33 by thrieg #+# #+# */
|
||||||
|
/* Updated: 2025/02/16 19:07:58 by thrieg ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "../libft.h"
|
||||||
|
#include "ft_printf.h"
|
||||||
|
|
||||||
|
int ft_case_x(t_vector *vec, va_list *args)
|
||||||
|
{
|
||||||
|
char *str_to_cat;
|
||||||
|
|
||||||
|
str_to_cat = ft_utoa_base(va_arg(*args, unsigned int), "0123456789abcdef");
|
||||||
|
if (!ft_vector_concat(vec, str_to_cat, ft_strlen(str_to_cat)))
|
||||||
|
return (free(str_to_cat), 0);
|
||||||
|
free(str_to_cat);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
int ft_case_upperx(t_vector *vec, va_list *args)
|
||||||
|
{
|
||||||
|
char *str_to_cat;
|
||||||
|
|
||||||
|
str_to_cat = ft_utoa_base(va_arg(*args, unsigned int), "0123456789ABCDEF");
|
||||||
|
if (!ft_vector_concat(vec, str_to_cat, ft_strlen(str_to_cat)))
|
||||||
|
return (free(str_to_cat), 0);
|
||||||
|
free(str_to_cat);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
int ft_case_d(t_vector *vec, va_list *args)
|
||||||
|
{
|
||||||
|
char *str_to_cat;
|
||||||
|
|
||||||
|
str_to_cat = ft_itoa(va_arg(*args, int));
|
||||||
|
if (!ft_vector_concat(vec, str_to_cat, ft_strlen(str_to_cat)))
|
||||||
|
return (free(str_to_cat), 0);
|
||||||
|
free(str_to_cat);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
int ft_case_i(t_vector *vec, va_list *args)
|
||||||
|
{
|
||||||
|
char *str_to_cat;
|
||||||
|
|
||||||
|
str_to_cat = ft_itoa(va_arg(*args, int));
|
||||||
|
if (!ft_vector_concat(vec, str_to_cat, ft_strlen(str_to_cat)))
|
||||||
|
return (free(str_to_cat), 0);
|
||||||
|
free(str_to_cat);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
BIN
libft/ft_printf/ft_cases_mandatory_two.o
Normal file
BIN
libft/ft_printf/ft_cases_mandatory_two.o
Normal file
Binary file not shown.
66
libft/ft_printf/ft_padding_bonus.c
Executable file
66
libft/ft_printf/ft_padding_bonus.c
Executable file
|
|
@ -0,0 +1,66 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_padding_bonus.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/10/27 16:54:14 by thrieg #+# #+# */
|
||||||
|
/* Updated: 2025/02/16 19:07:59 by thrieg ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "../libft.h"
|
||||||
|
#include "ft_printf.h"
|
||||||
|
#include "ft_printf_bonus.h"
|
||||||
|
|
||||||
|
//len is the len of what's already printed
|
||||||
|
//returns the number of characters writen or -1 if an error occurs.
|
||||||
|
int padding_if_needed_before(t_arglist *arglist,
|
||||||
|
char caller,
|
||||||
|
size_t len,
|
||||||
|
t_vector *vec)
|
||||||
|
{
|
||||||
|
char padding_char;
|
||||||
|
int i;
|
||||||
|
int padding_len;
|
||||||
|
|
||||||
|
padding_len = arglist->width_mini - (int)len;
|
||||||
|
if (padding_len <= 0)
|
||||||
|
return (0);
|
||||||
|
if (arglist->flag_justified_left)
|
||||||
|
return (0);
|
||||||
|
if ((arglist->flag_zero == 1) && caller != 'c' && caller != 's'
|
||||||
|
&& caller != 'p' && (arglist->precision < 0))
|
||||||
|
padding_char = '0';
|
||||||
|
else
|
||||||
|
padding_char = ' ';
|
||||||
|
i = 0;
|
||||||
|
while (i++ < padding_len)
|
||||||
|
if (!ft_vector_pushback(vec, padding_char))
|
||||||
|
return (-1);
|
||||||
|
return (padding_len);
|
||||||
|
}
|
||||||
|
|
||||||
|
//len is the len of what's already printed
|
||||||
|
//returns the number of characters writen or -1 if an error occurs.
|
||||||
|
int padding_if_needed_after(t_arglist *arglist,
|
||||||
|
char caller,
|
||||||
|
size_t len,
|
||||||
|
t_vector *vec)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int padding_len;
|
||||||
|
|
||||||
|
(void)caller;
|
||||||
|
padding_len = arglist->width_mini - (int)len;
|
||||||
|
if (padding_len <= 0)
|
||||||
|
return (0);
|
||||||
|
if (!(arglist->flag_justified_left))
|
||||||
|
return (0);
|
||||||
|
i = 0;
|
||||||
|
while (i++ < padding_len)
|
||||||
|
if (!ft_vector_pushback(vec, ' '))
|
||||||
|
return (-1);
|
||||||
|
return (padding_len);
|
||||||
|
}
|
||||||
BIN
libft/ft_printf/ft_padding_bonus.o
Normal file
BIN
libft/ft_printf/ft_padding_bonus.o
Normal file
Binary file not shown.
126
libft/ft_printf/ft_printf.c
Executable file
126
libft/ft_printf/ft_printf.c
Executable file
|
|
@ -0,0 +1,126 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_printf.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/10/21 18:24:04 by thrieg #+# #+# */
|
||||||
|
/* Updated: 2025/02/16 19:08:03 by thrieg ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "../libft.h"
|
||||||
|
#include "ft_printf.h"
|
||||||
|
|
||||||
|
int ft_case_c(t_vector *vec, va_list *args);
|
||||||
|
int ft_case_percent(t_vector *vec);
|
||||||
|
int ft_case_s(t_vector *vec, va_list *args);
|
||||||
|
int ft_case_p(t_vector *vec, va_list *args);
|
||||||
|
int ft_case_u(t_vector *vec, va_list *args);
|
||||||
|
int ft_case_x(t_vector *vec, va_list *args);
|
||||||
|
int ft_case_upperx(t_vector *vec, va_list *args);
|
||||||
|
int ft_case_d(t_vector *vec, va_list *args);
|
||||||
|
int ft_case_i(t_vector *vec, va_list *args);
|
||||||
|
int handle_percent_bonus(const char *str, t_vector *vec, va_list *args);
|
||||||
|
|
||||||
|
//properly frees the vector and return string
|
||||||
|
void ft_cleanup(t_vector **vec, va_list *args)
|
||||||
|
{
|
||||||
|
ft_free_vector(vec);
|
||||||
|
va_end(*args);
|
||||||
|
}
|
||||||
|
|
||||||
|
//returns the number of characters to skip after the % sign
|
||||||
|
//or 0 if something goes wrong
|
||||||
|
int handle_percent(const char *str, t_vector *vec, va_list *args)
|
||||||
|
{
|
||||||
|
if (str[0] == '%' && str[1] == 'c')
|
||||||
|
return (ft_case_c(vec, args));
|
||||||
|
if (str[0] == '%' && str[1] == '%')
|
||||||
|
return (ft_case_percent(vec));
|
||||||
|
if (str[0] == '%' && str[1] == 's')
|
||||||
|
return (ft_case_s(vec, args));
|
||||||
|
if (str[0] == '%' && str[1] == 'p')
|
||||||
|
return (ft_case_p(vec, args));
|
||||||
|
if (str[0] == '%' && str[1] == 'u')
|
||||||
|
return (ft_case_u(vec, args));
|
||||||
|
if (str[0] == '%' && str[1] == 'x')
|
||||||
|
return (ft_case_x(vec, args));
|
||||||
|
if (str[0] == '%' && str[1] == 'X')
|
||||||
|
return (ft_case_upperx(vec, args));
|
||||||
|
if (str[0] == '%' && (str[1] == 'd'))
|
||||||
|
return (ft_case_d(vec, args));
|
||||||
|
if (str[0] == '%' && (str[1] == 'i'))
|
||||||
|
return (ft_case_i(vec, args));
|
||||||
|
return (handle_percent_bonus(str, vec, args));
|
||||||
|
}
|
||||||
|
|
||||||
|
static int ft_parsing_loop(
|
||||||
|
t_vector *return_vector,
|
||||||
|
const char *str,
|
||||||
|
va_list *args
|
||||||
|
)
|
||||||
|
{
|
||||||
|
char *next_percent;
|
||||||
|
int nb_character;
|
||||||
|
|
||||||
|
while (*str)
|
||||||
|
{
|
||||||
|
next_percent = ft_strchr(str, '%');
|
||||||
|
if (!next_percent)
|
||||||
|
{
|
||||||
|
next_percent = ft_strchr(str, '\0');
|
||||||
|
if (!ft_vector_concat(return_vector, str, (next_percent - str)))
|
||||||
|
return (0);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
if (!ft_vector_concat(return_vector, str, (next_percent - str)))
|
||||||
|
return (0);
|
||||||
|
str = next_percent;
|
||||||
|
nb_character = handle_percent(str, return_vector, args);
|
||||||
|
if (!nb_character)
|
||||||
|
return (0);
|
||||||
|
str += nb_character;
|
||||||
|
str++;
|
||||||
|
}
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
//returns the number of characters written or -1 on errors
|
||||||
|
int ft_printf(const char *str, ...)
|
||||||
|
{
|
||||||
|
int nb_character;
|
||||||
|
t_vector *return_vector;
|
||||||
|
va_list args;
|
||||||
|
|
||||||
|
if (!str)
|
||||||
|
return (-1);
|
||||||
|
va_start(args, str);
|
||||||
|
return_vector = ft_create_vector(2);
|
||||||
|
if (!return_vector)
|
||||||
|
return (-1);
|
||||||
|
if (!ft_parsing_loop(return_vector, str, &args))
|
||||||
|
return (ft_cleanup(&return_vector, &args), -1);
|
||||||
|
nb_character = write(1, return_vector->buffer, return_vector->index);
|
||||||
|
return (ft_cleanup(&return_vector, &args), nb_character);
|
||||||
|
}
|
||||||
|
|
||||||
|
//returns the number of characters written or -1 on errors
|
||||||
|
int ft_dprintf(int fd, const char *str, ...)
|
||||||
|
{
|
||||||
|
int nb_character;
|
||||||
|
t_vector *return_vector;
|
||||||
|
va_list args;
|
||||||
|
|
||||||
|
if (!str)
|
||||||
|
return (-1);
|
||||||
|
va_start(args, str);
|
||||||
|
return_vector = ft_create_vector(2);
|
||||||
|
if (!return_vector)
|
||||||
|
return (-1);
|
||||||
|
if (!ft_parsing_loop(return_vector, str, &args))
|
||||||
|
return (ft_cleanup(&return_vector, &args), -1);
|
||||||
|
nb_character = write(fd, return_vector->buffer, return_vector->index);
|
||||||
|
return (ft_cleanup(&return_vector, &args), nb_character);
|
||||||
|
}
|
||||||
22
libft/ft_printf/ft_printf.h
Executable file
22
libft/ft_printf/ft_printf.h
Executable file
|
|
@ -0,0 +1,22 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_printf.h :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/10/14 12:38:59 by thrieg #+# #+# */
|
||||||
|
/* Updated: 2025/02/16 19:08:20 by thrieg ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#ifndef FT_PRINTF_H
|
||||||
|
# define FT_PRINTF_H
|
||||||
|
|
||||||
|
# include <stdarg.h>
|
||||||
|
# include "../ft_vector.h"
|
||||||
|
|
||||||
|
int ft_printf(const char *format, ...);
|
||||||
|
int ft_dprintf(int fd, const char *format, ...);
|
||||||
|
|
||||||
|
#endif
|
||||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue