fixed some stuff (100%)
This commit is contained in:
1
main.h
1
main.h
@@ -3,6 +3,7 @@
|
|||||||
int _printf(const char *format, ...);
|
int _printf(const char *format, ...);
|
||||||
int _contains(const char *str, char c);
|
int _contains(const char *str, char c);
|
||||||
int _strlen(const char *str);
|
int _strlen(const char *str);
|
||||||
|
char *_strcpy(char *dest, char *src);
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
19
printf.c
19
printf.c
@@ -11,7 +11,7 @@
|
|||||||
**/
|
**/
|
||||||
int _printf(const char *format, ...)
|
int _printf(const char *format, ...)
|
||||||
{
|
{
|
||||||
int i, j;
|
int buff_idx;
|
||||||
unsigned int identifiers, BUFF_SIZE;
|
unsigned int identifiers, BUFF_SIZE;
|
||||||
char *buffer, *next;
|
char *buffer, *next;
|
||||||
va_list args;
|
va_list args;
|
||||||
@@ -20,22 +20,21 @@ int _printf(const char *format, ...)
|
|||||||
va_start(args, format);
|
va_start(args, format);
|
||||||
identifiers = _contains(format, '%');
|
identifiers = _contains(format, '%');
|
||||||
BUFF_SIZE = _strlen(format) - (identifiers * 2);
|
BUFF_SIZE = _strlen(format) - (identifiers * 2);
|
||||||
|
buffer = malloc(BUFF_SIZE);
|
||||||
|
|
||||||
if (!format) /* No string. No laundry */
|
if (!format) /* No string. No laundry */
|
||||||
return (0);
|
return (0);
|
||||||
i = 0; /*TODO:gotta rename those*/
|
buff_idx = 0;
|
||||||
j = 0;
|
|
||||||
while (format)
|
while (format)
|
||||||
{
|
{
|
||||||
if ((*format == '%') && (*(format + 1)))
|
if ((*format == '%') && (*(format + 1))) /*hello %s*/
|
||||||
{
|
{
|
||||||
switch (*(format + 1)) /*this needs to shrink*/
|
switch (*(format + 1)) /*this needs to shrink*/
|
||||||
{
|
{
|
||||||
case 's':
|
case 's':
|
||||||
next = va_arg(args, char*); /*Store string temporarily*/
|
next = va_arg(args, char*); /*Store string temporarily*/
|
||||||
_strcpy(&buffer[j], next);
|
buffer = _strcpy(buffer, next);
|
||||||
j += _strlen(next);
|
BUFF_SIZE += _strlen(buffer);
|
||||||
BUFF_SIZE += _strlen(next);
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
case 'c': /* add 1 byte and i++ */
|
case 'c': /* add 1 byte and i++ */
|
||||||
@@ -46,11 +45,11 @@ int _printf(const char *format, ...)
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
*(buffer + i) = *(format + i);
|
*(buffer + buff_idx) = *(format + buff_idx);
|
||||||
i++;
|
buff_idx++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
write(1, buffer, BUFF_SIZE);
|
write(1, &buffer, BUFF_SIZE);
|
||||||
return (_strlen(buffer));
|
return (_strlen(buffer));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
*
|
*
|
||||||
* @str: Address to the head of the string (Array of Characters) (Lost btw)
|
* @str: Address to the head of the string (Array of Characters) (Lost btw)
|
||||||
*
|
*
|
||||||
* Returns: Length of String
|
* Return: Length of String
|
||||||
*/
|
*/
|
||||||
int _strlen(const char *str)
|
int _strlen(const char *str)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
#include<../main.h>
|
#include "../main.h"
|
||||||
#include<string.h>
|
#include <string.h>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* main - Tests if '%s' works within our printf.
|
* main - Tests if '%s' works within our printf.
|
||||||
@@ -10,10 +10,9 @@
|
|||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
char test_0[] = "Hej";
|
char test_0[] = "Hej";
|
||||||
char test_1[] = "H,e,j";
|
|
||||||
/*
|
/*
|
||||||
* Brain ded to think bout test cond
|
* Brain ded to think bout test cond
|
||||||
*/
|
*/
|
||||||
|
return (_printf("%s", test_0));
|
||||||
|
|
||||||
return (0);
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user