Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added courses/cunix2/ft_printf/printf.a
Binary file not shown.
24 changes: 24 additions & 0 deletions courses/cunix2/ft_printf/printf.h
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);
Comment on lines +4 to +22
Copy link
Copy Markdown

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


#endif
2 changes: 1 addition & 1 deletion courses/cunix2/ft_printf/printf_test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ FT_PRINTF_PATH = ../

CC = gcc
CCFLAGS = -Wall -Wextra -Wno-trigraphs # Adding -std=c99 causes crash
LDFLAGS = -L$(FT_PRINTF_PATH) -lftprintf
LDFLAGS = -L$(FT_PRINTF_PATH) -l:printf.a

NAME = ft_printf_test
CHECK_LEAKS_NAME = check_leaks
Expand Down
31 changes: 31 additions & 0 deletions courses/cunix2/ft_printf/src/ft_atoi.c
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
Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's better to start from the first digit and do sum += sum*10 + number instead of going from last digit to first and doing *= 10 in a loop

return res * sign;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

empty line before return

}
27 changes: 27 additions & 0 deletions courses/cunix2/ft_printf/src/ft_itoa.c
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)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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

}
104 changes: 104 additions & 0 deletions courses/cunix2/ft_printf/src/ft_printf.c
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)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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);
}

16 changes: 16 additions & 0 deletions courses/cunix2/ft_printf/src/ft_strlen.c
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;
}
24 changes: 24 additions & 0 deletions courses/cunix2/ft_printf/src/my_pad_with_zeros.c
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);
}

51 changes: 51 additions & 0 deletions courses/cunix2/ft_printf/src/my_print_buf.c
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);
}
}
}
10 changes: 10 additions & 0 deletions courses/cunix2/ft_printf/src/my_print_char.c
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));
Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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);
}
7 changes: 7 additions & 0 deletions courses/cunix2/ft_printf/src/my_print_str.c
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);
}
73 changes: 73 additions & 0 deletions courses/cunix2/libft/libft.h
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
6 changes: 6 additions & 0 deletions courses/cunix2/libft/src/ft_abs.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "../libft.h"

int abs(int i)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ft_abs

{
return i < 0 ? -i : i;
}
Loading