-
Notifications
You must be signed in to change notification settings - Fork 0
CUNIX_2 #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
CUNIX_2 #7
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| #ifndef LIB_H | ||
| #define LIB_H | ||
|
|
||
| #include <stdlib.h> | ||
| #include <stdarg.h> | ||
| #include <unistd.h> | ||
|
|
||
| void ft_printf(char *format, ...); | ||
|
|
||
| char *ft_itoa(int val, int base); | ||
|
|
||
| unsigned int ft_strlen(char *str); | ||
|
|
||
| int ft_atoi(const char *str); | ||
|
|
||
| void my_print_buf(char *buf, int align, int len); | ||
|
|
||
| void my_print_char(char ch, int align); | ||
|
|
||
| void my_print_str(char *str, int align); | ||
|
|
||
| void my_pad_with_zeros(int num, int len); | ||
|
|
||
| #endif | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| #include "../printf.h" | ||
|
|
||
| int ft_atoi(const char *str) | ||
| { | ||
| char ch; | ||
| int count = 0, res = 0, digit; | ||
| int sign = 1; | ||
| if ((ch = *str) == '-') | ||
|
Comment on lines
+7
to
+8
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good to split declarations and code with an empty line |
||
| { | ||
| str++; | ||
| sign = -1; | ||
| } | ||
|
|
||
| while ((ch = *str) >= 48 && ch <= 57) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 48 and 57 are magic numbers, change to '0' and '9' |
||
| { | ||
| str++; | ||
| count++; | ||
| } | ||
| int power = 0; | ||
| while ((power++) < count) | ||
| { | ||
| int multiple = 1; | ||
| digit = *(--str) - 48; | ||
| for (int i = 1; i < power; i++) | ||
| { | ||
| multiple *= 10; | ||
| } | ||
| res += digit * multiple; | ||
| } | ||
|
Comment on lines
+20
to
+29
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's better to start from the first digit and do |
||
| return res * sign; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. empty line before return |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| char *ft_itoa(int val, int base) | ||
| { | ||
| char temp[24]; | ||
| int pos_temp = 0, pos_res = 0; | ||
| static char res[20]; | ||
| int count = 0; | ||
| if (val < 0) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. empty line before if |
||
| { | ||
| val *= -1; | ||
| res[pos_res++] = '-'; | ||
| } | ||
|
|
||
| do | ||
| { | ||
| temp[pos_temp++] = val % base + '0'; | ||
| val /= base; | ||
| count++; | ||
| } | ||
| while (val > 0); | ||
|
|
||
| while (count-- > 0) | ||
| { | ||
| res[pos_res++] = temp[--pos_temp]; | ||
| } | ||
| res[pos_res] = '\0'; | ||
| return res; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So its a pointer to a static array, which means that if I call it twice my previous result will be overwritten |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| #include "../printf.h" | ||
|
|
||
| void ft_printf(char *format, ...) | ||
| { | ||
| va_list list; | ||
| va_start(list, format); | ||
| int num = 0; | ||
| int format_mode = 0; | ||
| int align_len = 0; | ||
| int plus_found = 0, minus_found = 0, number_found = 0, space_found = 0, zero_found = 0; | ||
| char *parsed = NULL; | ||
| while (*format) | ||
| { | ||
| if (format_mode) | ||
| { | ||
| switch (*format) | ||
| { | ||
| case '%': | ||
| write(1, "%", 1); | ||
| format_mode = 0; | ||
| break; | ||
| case ' ': | ||
| space_found = 1; | ||
| break; | ||
| case 'd': | ||
| case 'i': | ||
| num = va_arg(list, int); | ||
| if (zero_found) | ||
| { | ||
| my_pad_with_zeros(num, align_len); | ||
| break; | ||
| } | ||
| if (space_found && num >= 0 && !align_len) | ||
| { | ||
| my_print_str(" ", 0); | ||
| } | ||
| if (plus_found && num >= 0) | ||
| { | ||
| my_print_str("+", 0); | ||
| } | ||
| parsed = ft_itoa(num, 10); | ||
| my_print_str(parsed, align_len); | ||
| align_len = format_mode = number_found = 0; | ||
| break; | ||
| case 's': | ||
| my_print_str(va_arg(list, char *), align_len); | ||
| align_len = format_mode = number_found = 0; | ||
| break; | ||
| case 'c': | ||
| my_print_char(va_arg(list, int), align_len); | ||
| align_len = format_mode = number_found = 0; | ||
| break; | ||
| case '+': | ||
| plus_found = 1; | ||
| break; | ||
| case '-': | ||
| minus_found = 1; | ||
| break; | ||
| default: | ||
| if (*format < 48 || *format > 59) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. magic numbers, should be chars |
||
| { | ||
| write(1, format, 1); | ||
| format_mode = 0; | ||
| } | ||
| else | ||
| { | ||
| if (!number_found) | ||
| { | ||
| if (*format == '0') | ||
| { | ||
| zero_found = 1; | ||
| } | ||
| number_found = 1; | ||
| if (minus_found) | ||
| { | ||
| align_len = (-1) * ft_atoi(format); | ||
| minus_found = 0; | ||
| } | ||
| else if (plus_found || number_found) | ||
| { | ||
| align_len = ft_atoi(format); | ||
| plus_found = 0; | ||
| } | ||
| } | ||
| } | ||
| break; | ||
| } | ||
| } | ||
| else | ||
| { | ||
| if (*format == '%') | ||
| { | ||
| format_mode = 1; | ||
| } | ||
| else | ||
| { | ||
| write(1, format, 1); | ||
| } | ||
| } | ||
| format++; | ||
| } | ||
| va_end(list); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| #include "../printf.h" | ||
|
|
||
| unsigned int ft_strlen(char *str) | ||
| { | ||
| if (!str) | ||
| { | ||
| return 0; | ||
| } | ||
| int count = 0; | ||
| while (*str != '\0') | ||
| { | ||
| count++; | ||
| str++; | ||
| } | ||
| return count; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| #include "../printf.h" | ||
|
|
||
| void my_pad_with_zeros(int num, int len) | ||
| { | ||
| if (num < 0) | ||
| { | ||
| num *= -1; | ||
| write(1, "-", 1); | ||
| len--; | ||
| } | ||
| char *str = ft_itoa(num, 10); | ||
| size_t str_len = ft_strlen(str); | ||
| int pad = len - str_len; | ||
| if (pad < 0) | ||
| { | ||
| pad = 0; | ||
| } | ||
| while (pad--) | ||
| { | ||
| write(1, "0", 1); | ||
| } | ||
| write(1, str, str_len); | ||
| } | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| #include "../printf.h" | ||
|
|
||
| void my_print_buf(char *buf, int align, int len) | ||
| { | ||
| int pad = 0; | ||
| if (!buf) | ||
| { | ||
| len = 6; | ||
| } | ||
| if (align < 0) | ||
| { | ||
| align *= -1; | ||
| if (buf) | ||
| { | ||
| write(1, buf, len); | ||
| } | ||
| else | ||
| { | ||
| write(1, "(null)", 6); | ||
| } | ||
| pad = align - len; | ||
| if (pad < 0) | ||
| { | ||
| pad = 0; | ||
| } | ||
| while (pad--) | ||
| { | ||
| write(1, " ", 1); | ||
| } | ||
| } | ||
| else | ||
| { | ||
| pad = align - len; | ||
| if (pad < 0) | ||
| { | ||
| pad = 0; | ||
| } | ||
| while (pad--) | ||
| { | ||
| write(1, " ", 1); | ||
| } | ||
| if (buf) | ||
| { | ||
| write(1, buf, len); | ||
| } | ||
| else | ||
| { | ||
| write(1, "(null)", 6); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| #include "../printf.h" | ||
|
|
||
| void my_print_char(char ch, int align) | ||
| { | ||
| char *str_buf = malloc(2 * sizeof(char)); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why it does not do two writes? |
||
| str_buf[0] = ch; | ||
| str_buf[1] = '\0'; | ||
| my_print_buf(str_buf, align, 1); | ||
| free(str_buf); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| #include "../printf.h" | ||
|
|
||
| void my_print_str(char *str, int align) | ||
| { | ||
| size_t len = ft_strlen(str); | ||
| my_print_buf(str, align, len); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| #ifndef LIBFT_H | ||
| #define LIBFT_H | ||
|
|
||
| #include <stddef.h> | ||
| #include <stdlib.h> | ||
|
|
||
| void ft_bzero(void *s, size_t n); | ||
|
|
||
| char *ft_strdup(const char *s); | ||
|
|
||
| unsigned int ft_strlen(const char *str); | ||
|
|
||
| int ft_strncmp(const char *s1, const char *s2, size_t n); | ||
|
|
||
| char *ft_strchr(const char *s, int c); | ||
|
|
||
| char *ft_strrchr(const char *s, int c); | ||
|
|
||
| int ft_isalpha (int c); | ||
|
|
||
| int ft_isdigit(int c); | ||
|
|
||
| int ft_isascii(int c); | ||
|
|
||
| int ft_toupper(int c); | ||
|
|
||
| int ft_tolower(int c); | ||
|
|
||
| char *ft_strstr(const char *haystack, const char *needle); | ||
|
|
||
| char *ft_strnstr(const char *hay, const char *need, size_t len); | ||
|
|
||
| int abs(int i); | ||
|
|
||
| div_t ft_div(int num, int denom); | ||
|
|
||
| void *ft_memalloc(size_t size); | ||
|
|
||
| void *ft_memccpy(void *dst, const void *src, int c, size_t n); | ||
|
|
||
| void *ft_memchr(const void *s, int c, size_t n); | ||
|
|
||
| int ft_memcmp(const void *s1, const void *s2, size_t n); | ||
|
|
||
| void *ft_memcpy(void *destination, const void *src, size_t n); | ||
|
|
||
| void *ft_memmove(void *dst, const void *src, size_t len); | ||
|
|
||
| void *ft_memset(void *destination, int c, size_t n); | ||
|
|
||
| char *ft_strcat(char *s1, const char *s2); | ||
|
|
||
| char *ft_strcpy(char *dst, const char *src); | ||
|
|
||
| void ft_striter(char *s, void (*f)(char *)); | ||
|
|
||
| char *ft_strjoin(char const *s1, char const *s2); | ||
|
|
||
| char *ft_strmap(char const *s, char (*f)(char)); | ||
|
|
||
| char *ft_strnew(size_t size); | ||
|
|
||
| char **ft_strsplit(char const *s, char c); | ||
|
|
||
| char *ft_strsub(char const *s, unsigned int start, size_t len); | ||
|
|
||
| char *ft_strtrim(char const *s); | ||
|
|
||
| int ft_strwhitespace(char c); | ||
|
|
||
| int ft_count(char const *str, char c); | ||
|
|
||
| #endif |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| #include "../libft.h" | ||
|
|
||
| int abs(int i) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ft_abs |
||
| { | ||
| return i < 0 ? -i : i; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I rarely see anyone adding spacing inside include guards