Nothing to see here

This commit is contained in:
2023-08-22 23:14:13 +03:00
parent 0f9928b611
commit 2d212866ec
28 changed files with 735 additions and 76 deletions

7
proven-point/README.md Normal file
View File

@@ -0,0 +1,7 @@
# printf
~~This took printf("Hello world") and made it difficult~~
Group project to implement printf from scratch
## Structure
`test/` where all the tests lay
## Building **TODO**

100
proven-point/itoa.c Normal file
View File

@@ -0,0 +1,100 @@
#include "main.h"
#include <stdlib.h>
/**
* _str_reverse - reverse a string because I said so
* @str: da string
* Return: number in string form
*/
char *_str_reverse(char *str)
{
unsigned int i, len;
char *buffer;
len = _strlen(str);
buffer = malloc(len);
if (!buffer)
return (NULL);
for (i = 0; i < len; i++)
*(buffer + len - 1 - i) = *(str + i);
return (buffer);
}
/**
* _itoa - turn NUMBA into a STRING
* @n: NUMBA
* @base: base
* Return: numba in string form
*/
char *_itoa(int n, int base)
{
int i, isNegative, rem;
char *str;
str = malloc(1024);
i = isNegative = 0;
if (n == 0)
{
*(str + i++) = '0';
*(str + i) = '\0';
return (str);
}
if (n < 0 && base == 10)
{
isNegative = 1;
n = -n;
}
while (n != 0)
{
rem = n % base;
*(str + i++) = (rem > 9) ? (rem - 10) + 'a' : rem + '0';
n = n / base;
}
if (isNegative)
*(str + i++) = '-';
*(str + i) = '\0';
str = _str_reverse(str);
return (str);
}
/**
* _uitoa - turn NUMBA into a STRING
* @n: NUMBA
* @base: base
* Return: numba in string form
*/
char *_uitoa(unsigned int n, int base)
{
int i, rem;
char *str;
str = malloc(1024);
i = 0;
if (n == 0)
{
*(str + i++) = '0';
*(str + i) = '\0';
return (str);
}
while (n != 0)
{
rem = n % base;
*(str + i++) = (rem > 9) ? (rem - 10) + 'a' : rem + '0';
n = n / base;
}
*(str + i) = '\0';
str = _str_reverse(str);
return (str);
}

18
proven-point/main.h Normal file
View File

@@ -0,0 +1,18 @@
#ifndef MAIN_H_
#define MAIN_H_
#include <stdarg.h>
int _printf(const char *format, ...);
int _contains(const char *str, char c);
int _strlen(const char *str);
char *_strcpy(char *dest, char *src);
char *append(char *str, char c);
int _puts(char *str);
int _putchar(char c);
char *_memset(char *adr, int bval);
int fmt(char c, va_list args);
char *_str_reverse(char *str);
char *_itoa(int n, int base);
char *_uitoa(unsigned int n, int base);
char *str_up(char *s);
#endif

22
proven-point/memset.c Normal file
View File

@@ -0,0 +1,22 @@
#include "main.h"
/**
* _memset - sets values of bytes to specific value
*
* @adr: head address
* @bval: number of bytes
*
* Return: pointer to place
*/
char *_memset(char *adr, int bval)
{
int i;
for (i = 0; i < _strlen(adr); i++)
{
*(adr + i) = bval;
}
return (adr);
}

90
proven-point/printf.c Normal file
View File

@@ -0,0 +1,90 @@
#include "main.h"
#include <stdlib.h>
#include <unistd.h>
/**
* _printf - printf but worse
* @format: format string
* @...: ???
* Return: characters written
**/
int _printf(const char *format, ...)
{
unsigned int buff_idx, fmt_idx, buff_size, printed;
char *buffer; /*where non formated things are stored*/
va_list args;
va_start(args, format);
buff_size = _strlen(format) - _contains(format, '%');
buffer = (char *) malloc(buff_size); /* sized of the non % instances only*/
if (!format && !buffer) /* No string. No laundry */
return (0);
buff_idx = fmt_idx = printed = 0; /*chain assignment*/
while (*(format + fmt_idx))
{
if ((*(format + fmt_idx) == '%') && (*(format + fmt_idx + 1)))
{
if (buffer) /* printing and clearing buffer on formatted things */
{
printed += _puts(buffer);
buff_size -= _strlen(buffer);
buffer = _memset(buffer, 0);
buffer = (char *) malloc(buff_size);
if (!buffer)
return (-1);
buff_idx = 0;
}
printed += fmt(*(format + fmt_idx + 1), args);
fmt_idx += 2;
}
else
*(buffer + buff_idx++) = *(format + fmt_idx++); /* filling up buffer */
}
if (buffer)
{
printed += _puts(buffer);
buffer = _memset(buffer, 0);
free(buffer);
}
return (printed);
}
/**
* fmt - format because its too chonky for printf
* @c: format character (for now)
* @args: the arguments to pop from
* Return: bytes written to stdout
*/
int fmt(char c, va_list args)
{
switch (c) /*this needs to shrink*/
{
case 's':
return (_puts(va_arg(args, char*)));
case 'c':
return (_putchar(va_arg(args, int)));
case '%': /*add 1 byte*/
return (_putchar('%'));
case 'd':
return (_puts(_itoa(va_arg(args, int), 10)));
case 'i':
return (_puts(_uitoa(va_arg(args, unsigned int), 10)));
case 'u':
return (_puts(_uitoa(va_arg(args, unsigned int), 10)));
case 'b':
return (_puts(_uitoa(va_arg(args, unsigned int), 2)));
case 'o':
return (_puts(_uitoa(va_arg(args, unsigned int), 8)));
case 'x':
return (_puts(_uitoa(va_arg(args, unsigned int), 16)));
case 'X':
return (_puts(str_up(_uitoa(va_arg(args, unsigned int), 16))));
case 'p':
return (_puts(_uitoa(va_arg(args, unsigned int), 16)));
default:
_putchar('%'); /* TODO make this cleaner */
_putchar(c);
return (2);
}
}

26
proven-point/puts.c Normal file
View File

@@ -0,0 +1,26 @@
#include "main.h"
#include <unistd.h>
/**
* _puts - splurges whatever is in buffer
*
* @str: pointers to "buffer" or string head
*
* Return: written bytes
*/
int _puts(char *str)
{
return (write(1, str, _strlen(str)));
}
/**
* _putchar - writes the character c to stdout
* @c: The character to print
*
* Return: On success 1.
* On error, -1 is returned, and errno is set appropriately.
*/
int _putchar(char c)
{
return (write(1, &c, 1));
}

27
proven-point/strcpy.c Normal file
View File

@@ -0,0 +1,27 @@
#include "main.h"
#include <stdlib.h>
/**
* _strcpy - strcpy with an n
* @dest: the buffer we copying to
* @src: the idot we ctrl+c-ed
* Return: dest
*/
char *_strcpy(char *dest, char *src)
{
int s_idx, i, strlen;
char *new_me;
new_me = malloc(_strlen(dest) + _strlen(src) + 1);
strlen = _strlen(src);
s_idx = -1;
while (*(dest + ++s_idx))
*(new_me + s_idx) = *(dest + s_idx);
for (i = 0; i < strlen; i++)
*(new_me + s_idx++) = *(src + i);
*(new_me + _strlen(new_me)) = '\0';
return (new_me);
}

84
proven-point/strings.c Normal file
View File

@@ -0,0 +1,84 @@
#include "main.h"
#include <stdlib.h>
/**
* _strlen - Takes string and return its length
*
* @str: Address to the head of the string (Array of Characters) (Lost btw)
*
* Return: Length of String
*/
int _strlen(const char *str)
{
if (*str)
return (1 + _strlen(++str));
else
return (0);
}
/**
* _contains - gets number of unique identifiers
*
* @str: da string
* @c: basically almost always '%'
*
* Return: number of unique cases of "%*" that aren't "%%"
*/
int _contains(const char *str, char c)
{
if (*str)
{
if (*str == c && *(str + 1) != c && *(str - 1) != c)
return (1 + _contains(str + 1, c));
else
return (_contains(str + 1, c));
}
else
return (0);
}
/**
* append - shoved character at the end
*
* @str: main string
* @c: character getting shoved
*
* Return: da string but appended
*/
char *append(char *str, char c)
{
int len, i;
char *new_me;
len = _strlen(str);
new_me = malloc(len + 1);
i = -1;
while (*(str + ++i))
*(new_me + i) = *(str + i);
*(new_me + i++) = c;
*(new_me + len + 1) = '\0';
return (new_me);
}
/**
* str_up - changes all lowercase letters of a string
* to uppercase
* @s: string to modify
*
* Return: the resulting string
*/
char *str_up(char *s)
{
int i;
for (i = 0; s[i] != '\0'; i++)
{
if (s[i] >= 'a' && s[i] <= 'z')
s[i] = s[i] - 32;
}
return (s);
}

BIN
proven-point/tests/a Executable file

Binary file not shown.

View File

@@ -0,0 +1,17 @@
#include "../main.h"
#include <stdio.h>
/**
*main - ensure append work
*/
int main(void)
{
char *str, c;
str = "hello";
c = 'c';
str = append(str, c);
_printf("%s\n", str);
return (0);
}

View File

@@ -0,0 +1,15 @@
#include "../main.h"
/**
* main - testing printing of only char
*
* Return: deadth
*/
int main(void)
{
char c = 97;
_printf("cat: %css\noh wait is that...a %%\n", c);
return (0);
}

View File

@@ -0,0 +1,44 @@
#include "../main.h"
#include <limits.h>
#include <stdio.h>
/**
* main - Entry point
*
* Return: Always 0
*/
int main(void)
{
int len;
int len2;
unsigned int ui;
void *addr;
len = _printf("Let's try to printf a simple sentence.\n");
len2 = printf("Let's try to printf a simple sentence.\n");
ui = (unsigned int)INT_MAX + 1024;
addr = (void *)0x7ffe637541f0;
_printf("Length:[%d, %i]\n", len, len);
printf("Length:[%d, %i]\n", len2, len2);
_printf("Negative:[%d]\n", -762534);
printf("Negative:[%d]\n", -762534);
_printf("Unsigned:[%u]\n", ui);
printf("Unsigned:[%u]\n", ui);
_printf("Unsigned octal:[%o]\n", ui);
printf("Unsigned octal:[%o]\n", ui);
_printf("Unsigned hexadecimal:[%x, %X]\n", ui, ui);
printf("Unsigned hexadecimal:[%x, %X]\n", ui, ui);
_printf("Character:[%c]\n", 'H');
printf("Character:[%c]\n", 'H');
_printf("String:[%s]\n", "I am a string !");
printf("String:[%s]\n", "I am a string !");
_printf("Address:[%p]\n", addr);
printf("Address:[%p]\n", addr);
len = _printf("Percent:[%%]\n");
len2 = printf("Percent:[%%]\n");
_printf("Len:[%d]\n", len);
printf("Len:[%d]\n", len2);
_printf("Unknown:[%r]\n");
printf("Unknown:[%r]\n");
return (0);
}

View File

@@ -0,0 +1,13 @@
#include "../main.h"
/**
* main - Hello world
*
* Return: always 0
**/
int main(void)
{
int len;
len = _printf("Hello world\n");
return (len);
}

13
proven-point/tests/itoa.c Normal file
View File

@@ -0,0 +1,13 @@
#include "../main.h"
#define shit 0
/**
* main - check if this works
*/
int main(void)
{
char *str;
str = _itoa(10, 2);
_printf("%s", str);
return (shit);
}

View File

@@ -0,0 +1,14 @@
#include "../main.h"
#define shit 0
#define fr ;
/**
* main - no cap fr %d
*
*/
int main(void)
{
_printf("the number is: %d\n", 10)fr
_printf("the binary is: %b\n", 10)fr
_printf("the cursed i thing is: %i\n", 011);
return (shit)fr
}

12
proven-point/tests/puts.c Normal file
View File

@@ -0,0 +1,12 @@
#include "../main.h"
/**
* main - puts works?
*/
int main(void)
{
char *str;
str = "hello\n";
_puts(str);
return (0);
}

View File

@@ -0,0 +1,20 @@
#include "../main.h"
#include <string.h>
/**
* main - Tests if '%s' works within our printf.
*
* Return: 0 On Failure, 1 Otherwise Success
*/
int main(void)
{
char *test_0 = "Hej";
char *test_1 = "pls";
/*
* Brain ded to think bout test cond
*/
_printf(" suh %s\n%s\n", test_0, test_1);
return (0);
}

View File

@@ -0,0 +1,21 @@
#include "../main.h"
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
/**
* main - why does this not work! I know!
* Return: 0...unless?
*/
int main(void)
{
char *str, *str2, c;
str = "hello";
str2 = " world!\n";
str = _strcpy(str, str2);
c = 'c';
printf("%s", str);
write(1, &c, 1);
return (0);
}

View File

@@ -0,0 +1,19 @@
#include "../main.h"
/**
* main - test strlen
* Return: 0 if yes, 1 if no
*/
int main(void)
{
char *str;
int len;
str = "hello";
len =_strlen(str);
if (len == 5)
return (0);
else
return (1);
}

3
proven-point/tests/test.sh Executable file
View File

@@ -0,0 +1,3 @@
#!/usr/bin/bash
gcc -Wno-format -Wall -pedantic -Werror -Wextra -std=gnu89 $1 ../*.c -o a
./a