added debug features and colors to printf

This commit is contained in:
Jack Halford 2016-12-07 18:27:07 +01:00
parent 8dced91fb3
commit cf73ec7bd3
35 changed files with 291 additions and 100 deletions

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/16 11:13:15 by jhalford #+# #+# */
/* Updated: 2016/11/28 14:44:55 by jhalford ### ########.fr */
/* Updated: 2016/12/05 11:53:30 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -15,8 +15,6 @@
# include "libft.h"
typedef struct s_btree t_btree;
struct s_btree
{
void *item;
@ -34,23 +32,23 @@ struct s_printdata
int right;
};
typedef struct s_btree t_btree;
typedef struct s_printdata t_printdata;
t_btree *btree_create_node(void const *item, size_t content_size);
void btree_insert_data(
t_btree **root,
void *item,
size_t content_size,
int (*cmpf)(void *, void *));
void *btree_search_item(t_btree *root,
void *data_ref, int (*cmpf)(void *, void *));
int btree_level_count(t_btree *root);
void btree_apply_prefix(t_btree *root, void (*applyf)(void *));
void btree_apply_infix(t_btree *root, void (*applyf)(void *));
void btree_apply_suffix(t_btree *root, void (*applyf)(void *));
void btree_print(int fd, t_btree *tree, char *(*printer)(void *));
void btree_del(t_btree **root, void (*del)(void *, size_t));
void btree_delone(t_btree **root, void (*del)(void *, size_t));
#endif

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/25 13:36:48 by jhalford #+# #+# */
/* Updated: 2016/11/25 18:27:28 by jhalford ### ########.fr */
/* Updated: 2016/12/06 20:15:42 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,6 +14,14 @@
# define COLOR_H
# include "libft.h"
struct s_color
{
char fg[7];
char bg[7];
};
typedef struct s_color t_color;
# define FG_BLACK "\x1b[30m"
# define FG_RED "\x1b[31m"
# define FG_GREEN "\x1b[32m"
@ -32,7 +40,12 @@
# define BG_CYAN "\x1b[46m"
# define BG_DEFAULT "\x1b[49m"
# define FBG_DEFAULT "\x1b[49m\x1b[20m"
void ft_color_reset(void);
void ft_color_set(char *fg, char *bg);
void ft_color_set(t_color color);
void ft_color_mk(t_color *color, char fg[7], char bg[7]);
void ft_color_mkif(t_color *color, int cond, char fg[7], char bg[7]);
#endif

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/07 13:22:54 by jhalford #+# #+# */
/* Updated: 2016/11/21 18:20:10 by jhalford ### ########.fr */
/* Updated: 2016/12/06 19:12:15 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -49,6 +49,7 @@ extern t_conv g_convs[];
t_fmt *ft_fmt_init(void);
void ft_fmt_print(t_fmt *fmt);
void ft_printf_color(int fd, char **final, char **str);
t_fmt *ft_printf_parse(char **format, va_list ap);
void ft_printf_parse_flags(t_fmt *fmt, char **format);

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/05 12:21:36 by jhalford #+# #+# */
/* Updated: 2016/11/17 13:18:28 by jhalford ### ########.fr */
/* Updated: 2016/12/06 15:33:20 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,13 +6,18 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/07 13:49:04 by jhalford #+# #+# */
/* Updated: 2016/11/28 15:20:11 by jhalford ### ########.fr */
/* Updated: 2016/12/07 16:54:11 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef LIBFT_H
# define LIBFT_H
# define DG_MSG0 "{yel}%13s:{blu}%-3d{eoc} |"
# define DG_MSG1 ft_path_notdir(__FILE__), __LINE__
# define DG(f, ...) ft_dprintf(2, DG_MSG0 f "{eoc}\n", DG_MSG1, ##__VA_ARGS__)
# include "get_next_line.h"
# include "ft_xattr.h"
# include "mytime.h"
# include "lst.h"
@ -40,9 +45,14 @@
# define STDOUT 1
# define STDERR 2
typedef long long t_type;
typedef struct s_stof t_stof;
typedef struct s_stos t_stos;
struct s_stos
{
char *key;
char *val;
};
struct s_stof
{

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/07 13:27:46 by jhalford #+# #+# */
/* Updated: 2016/11/23 14:50:54 by jhalford ### ########.fr */
/* Updated: 2016/12/07 16:54:25 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -54,6 +54,7 @@ void ft_lst_delsub(
t_list *sub, int (*cmp)(),
void (*del)(void *, size_t));
void ft_lst_cfree(void *ptr, size_t size);
void ft_lst_bfree(void *ptr, size_t size);
t_list *ft_lst_filter(
t_list *lst,
void const *data_ref,

View file

@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* btree_create_node.c :+: :+: :+: */
/* btree_apply_suffix.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/08/16 13:43:51 by jhalford #+# #+# */
/* Updated: 2016/11/14 16:08:23 by jhalford ### ########.fr */
/* Created: 2016/12/05 11:43:08 by jhalford #+# #+# */
/* Updated: 2016/12/05 11:43:13 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* btree_del.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/05 11:43:03 by jhalford #+# #+# */
/* Updated: 2016/12/05 11:53:24 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "btree.h"
void btree_del(t_btree **root, void (*del)(void *, size_t))
{
if (root && *root && del)
{
btree_del(&(*root)->left, del);
btree_del(&(*root)->right, del);
btree_delone(root, del);
}
}

View file

@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* btree_delone.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/05 11:45:51 by jhalford #+# #+# */
/* Updated: 2016/12/05 14:07:12 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "btree.h"
void btree_delone(t_btree **root, void (*del)(void *, size_t))
{
if (root && *root)
{
if (del)
(*del)((*root)->item, (*root)->content_size);
free(*root);
*root = NULL;
}
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/14 18:06:24 by jhalford #+# #+# */
/* Updated: 2016/11/28 14:53:46 by jhalford ### ########.fr */
/* Updated: 2016/12/06 20:10:51 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -75,6 +75,8 @@ void btree_print(int fd, t_btree *tree, char *(*printer)(void *))
{
if (ft_strcmp(s[i], empty) == 0)
break ;
if (i == 0 && ft_strcmp(s[1], empty) == 0)
break ;
ft_dprintf(fd, "%s\n", s[i]);
}
}

View file

@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_color_mk.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/27 11:35:18 by jhalford #+# #+# */
/* Updated: 2016/12/03 12:16:59 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_color_mk(t_color *color, char fg[7], char bg[7])
{
ft_strcpy(color->fg, fg);
ft_strcpy(color->bg, bg);
}

View file

@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_color_mk.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/27 11:35:18 by jhalford #+# #+# */
/* Updated: 2016/12/03 12:17:14 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_color_mkif(t_color *color, int cond, char fg[7], char bg[7])
{
if (cond)
ft_color_mk(color, fg, bg);
}

View file

@ -6,14 +6,17 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/25 13:53:46 by jhalford #+# #+# */
/* Updated: 2016/11/25 14:05:39 by jhalford ### ########.fr */
/* Updated: 2016/12/03 12:16:38 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_color_set(char *fg, char *bg)
void ft_color_set(t_color color)
{
ft_putstr(fg);
ft_putstr(bg);
char out[20];
ft_strcpy(out, color.fg);
ft_strcat(out, color.bg);
ft_putstr(out);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/14 17:55:40 by jhalford #+# #+# */
/* Updated: 2016/11/16 11:15:40 by jhalford ### ########.fr */
/* Updated: 2016/12/07 17:29:54 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/07 13:27:20 by jhalford #+# #+# */
/* Updated: 2016/11/07 13:27:20 by jhalford ### ########.fr */
/* Updated: 2016/12/07 17:35:12 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/07 13:33:27 by jhalford #+# #+# */
/* Updated: 2016/11/21 18:22:26 by jhalford ### ########.fr */
/* Updated: 2016/12/07 17:14:27 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -52,7 +52,9 @@ int ft_vdprintf(int fd, const char *format, va_list ap)
while (*str)
{
tmp = final;
if (*str == '%')
if (*str == '{')
ft_printf_color(fd, &final, &str);
else if (*str == '%')
{
if (ft_fmtcalc(&final, &str, ap))
return (1);

View file

@ -0,0 +1,45 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf_color.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/06 18:07:39 by jhalford #+# #+# */
/* Updated: 2016/12/07 13:48:34 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
t_stos g_colors[] =
{
{"{bla}", FG_BLACK},
{"{red}", FG_RED},
{"{gre}", FG_GREEN},
{"{yel}", FG_YELLOW},
{"{blu}", FG_BLUE},
{"{mag}", FG_MAGENTA},
{"{cya}", FG_CYAN},
{"{eoc}", FG_DEFAULT},
{NULL, NULL},
};
void ft_printf_color(int fd, char **final, char **str)
{
int i;
i = 0;
while(g_colors[i].key)
{
if (ft_strncmp(*str, g_colors[i].key, 5) == 0)
{
(void)fd;
/* if (isatty(fd)) */
*final = ft_strjoin(*final, g_colors[i].val);
*str += 5;
return ;
}
i++;
}
}

View file

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lst_bfree.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/07 16:55:04 by jhalford #+# #+# */
/* Updated: 2016/12/07 18:07:31 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_lst_bfree(void *ptr, size_t size)
{
t_list *lst;
lst = ptr;
(void)size;
if (lst)
{
ft_strdel((char **)&lst->content);
free(lst);
}
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/04 11:09:10 by jhalford #+# #+# */
/* Updated: 2016/11/08 11:09:49 by jhalford ### ########.fr */
/* Updated: 2016/12/07 16:55:43 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/04 11:09:12 by jhalford #+# #+# */
/* Updated: 2016/11/21 14:22:51 by jhalford ### ########.fr */
/* Updated: 2016/12/05 13:39:09 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/03 15:18:57 by jhalford #+# #+# */
/* Updated: 2016/11/21 14:02:16 by jhalford ### ########.fr */
/* Updated: 2016/12/05 13:39:14 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -18,6 +18,5 @@ void ft_lstdel(t_list **alst, void (*del)(void *, size_t))
{
ft_lstdel(&(*alst)->next, del);
ft_lstdelone(alst, del);
*alst = NULL;
}
}

View file

@ -3,10 +3,10 @@
/* ::: :::::::: */
/* ft_lstdelone.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/03 14:57:15 by jhalford #+# #+# */
/* Updated: 2016/11/28 19:13:36 by jhalford ### ########.fr */
/* Created: 2016/12/05 13:39:33 by jhalford #+# #+# */
/* Updated: 2016/12/05 13:43:03 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/03 14:57:25 by jhalford #+# #+# */
/* Updated: 2016/11/11 17:40:57 by jhalford ### ########.fr */
/* Updated: 2016/12/07 16:23:30 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/03 14:57:31 by jhalford #+# #+# */
/* Updated: 2016/11/11 17:39:00 by jhalford ### ########.fr */
/* Updated: 2016/12/07 15:34:47 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/04 11:45:16 by jhalford #+# #+# */
/* Updated: 2016/11/07 15:43:41 by jhalford ### ########.fr */
/* Updated: 2016/12/07 15:22:06 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/04 11:45:07 by jhalford #+# #+# */
/* Updated: 2016/11/23 15:46:28 by jhalford ### ########.fr */
/* Updated: 2016/12/07 15:22:09 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/03 18:03:58 by jhalford #+# #+# */
/* Updated: 2016/11/14 16:31:02 by jhalford ### ########.fr */
/* Updated: 2016/12/07 17:39:37 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -23,16 +23,12 @@ char **ft_sstradd(char **sstr, char *new)
if (sstr)
while (sstr[size])
size++;
if (!(newlist = (char **)malloc(sizeof(char *) * (size + 2))))
if (!(newlist = (char **)ft_memalloc(sizeof(char *) * (size + 2))))
return (NULL);
if (sstr)
while (sstr[i])
{
newlist[i] = sstr[i];
i++;
}
newlist[i++] = ft_strdup(new);
newlist[i] = NULL;
ft_memcpy(newlist, sstr, sizeof(char*) * size);
newlist[size++] = ft_strdup(new);
newlist[size] = NULL;
free(sstr);
return (newlist);
}

View file

@ -3,10 +3,10 @@
/* ::: :::::::: */
/* ft_sstrdup.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/03 18:04:13 by jhalford #+# #+# */
/* Updated: 2016/12/01 12:29:27 by jhalford ### ########.fr */
/* Created: 2016/12/07 14:25:45 by jhalford #+# #+# */
/* Updated: 2016/12/07 16:29:08 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -20,6 +20,7 @@ char **ft_sstrdup(char **list)
i = 0;
size = 0;
DG("sstrdup call now");
while (list[size])
size++;
cpy = (char **)malloc(sizeof(char *) * (size + 1));
@ -28,6 +29,7 @@ char **ft_sstrdup(char **list)
cpy[i++] = ft_strdup(*list);
list++;
}
DG("sstrdup i=%i, size=%i (should be equal)", i, size);
cpy[i] = NULL;
return (cpy);
}

View file

@ -3,10 +3,10 @@
/* ::: :::::::: */
/* ft_sstrfree.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/08 17:01:24 by jhalford #+# #+# */
/* Updated: 2016/11/14 11:08:19 by jhalford ### ########.fr */
/* Created: 2016/12/05 11:53:36 by jhalford #+# #+# */
/* Updated: 2016/12/05 16:38:33 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/08/07 10:48:12 by jhalford #+# #+# */
/* Updated: 2016/08/20 23:37:18 by jhalford ### ########.fr */
/* Updated: 2016/12/07 15:23:05 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -17,7 +17,7 @@ char *ft_strcpy(char *dst, const char *src)
int i;
i = 0;
while (src[i] != '\0')
while (src[i])
{
dst[i] = src[i];
i++;

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/03 14:58:00 by jhalford #+# #+# */
/* Updated: 2016/11/21 18:03:21 by jhalford ### ########.fr */
/* Updated: 2016/12/07 17:14:22 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,7 +14,7 @@
void ft_strdel(char **as)
{
if (as)
if (as && *as)
{
free(*as);
*as = NULL;

View file

@ -3,10 +3,10 @@
/* ::: :::::::: */
/* ft_strdup.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/03 14:58:01 by jhalford #+# #+# */
/* Updated: 2016/12/01 12:27:28 by jhalford ### ########.fr */
/* Created: 2016/12/07 14:25:41 by jhalford #+# #+# */
/* Updated: 2016/12/07 15:29:38 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -15,17 +15,10 @@
char *ft_strdup(const char *s1)
{
char *dup;
int size;
int i;
i = 0;
size = ft_strlen(s1);
dup = (char*)malloc(sizeof(*dup) * (size + 1));
while (s1[i])
{
dup[i] = s1[i];
i++;
}
dup[i] = '\0';
dup = (char*)malloc(sizeof(*dup) * (ft_strlen(s1) + 1));
ft_strcpy(dup, s1);
return (dup);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/03 14:58:18 by jhalford #+# #+# */
/* Updated: 2016/11/21 18:10:22 by jhalford ### ########.fr */
/* Updated: 2016/12/05 14:24:31 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -17,7 +17,9 @@ char *ft_strjoin(char const *s1, char const *s2)
char *join;
join = ft_strnew(ft_strlen(s1) + ft_strlen(s2) + 1);
if (s1)
ft_strcpy(join, s1);
if (s2)
ft_strcat(join, s2);
return (join);
}

View file

@ -6,13 +6,13 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/03 14:58:40 by jhalford #+# #+# */
/* Updated: 2016/11/07 13:07:01 by jhalford ### ########.fr */
/* Updated: 2016/12/05 17:20:45 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int countwords(char const *s, char c)
static int ft_countwords(char const *s, char c)
{
if (c == '\0')
return ((*s == '\0') ? 0 : 1);
@ -22,34 +22,49 @@ static int countwords(char const *s, char c)
return (0);
while (*s != c && *s != '\0')
s++;
return (1 + countwords(s, c));
return (1 + ft_countwords(s, c));
}
static int get_word_len(char const *str, char c)
{
int i;
int len;
i = 0;
len = 0;
while (str[i] == c)
i++;
while (str[i] != c && str[i] != '\0')
{
i++;
len++;
}
return (len);
}
char **ft_strsplit(char const *s, char c)
{
char **arr;
int i;
int j;
int w;
int k;
char **str2;
if (!s)
return (0);
w = countwords(s, c);
if ((arr = (char **)malloc((w + 1) * sizeof(char *))))
{
i = 0;
while (i < w)
{
while (*s == c)
++s;
if (!s || !(str2 = (char **)malloc(sizeof(*str2) *
(ft_countwords(s, c) + 1))))
return (NULL);
i = -1;
j = 0;
while (*(s + j) != c)
++j;
if ((arr[i] = ft_strnew(j)))
ft_strncpy(arr[i++], s, j);
s += j;
while (++i < ft_countwords(s, c))
{
k = 0;
if (!(str2[i] = ft_strnew(get_word_len(&s[j], c) + 1)))
str2[i] = NULL;
while (s[j] == c)
j++;
while (s[j] != c && s[j])
str2[i][k++] = s[j++];
str2[i][k] = '\0';
}
arr[i] = 0;
}
return (arr);
str2[i] = 0;
return (str2);
}

View file

@ -6,13 +6,12 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/25 11:34:56 by jhalford #+# #+# */
/* Updated: 2016/11/25 11:55:18 by jhalford ### ########.fr */
/* Updated: 2016/12/03 11:54:16 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
t_mytime *ft_mytime_get(time_t epoch)
{
char *date;
@ -30,4 +29,3 @@ t_mytime *ft_mytime_get(time_t epoch)
time->sec = ft_strsub(date, 17, 2);
return (time);
}