summaryrefslogtreecommitdiffstats
path: root/src/c_utils.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/c_utils.c')
-rw-r--r--src/c_utils.c159
1 files changed, 75 insertions, 84 deletions
diff --git a/src/c_utils.c b/src/c_utils.c
index 95ad28d..10013d7 100644
--- a/src/c_utils.c
+++ b/src/c_utils.c
@@ -21,34 +21,63 @@
#include "m_loop.h"
#include "s_struct.h"
-char
- *c_new_line(char *line, t_caps *tcaps)
+static unsigned int
+ c_redraw_next(size_t plen, unsigned int len, t_caps *tcaps)
{
- char *ret;
- size_t i;
+ unsigned int i;
+ unsigned int j;
- i = tcaps->lpos;
- ret = NULL;
- write(1, "\n", 1);
- tputs(tgetstr("cr", NULL), 1, ft_putchar);
- c_set_term_raw(0);
- if (line != NULL)
+ i = (tcaps->nlines == tcaps->lpos) ? 0 :
+ tcaps->ws.ws_col * (tcaps->lpos - 1);
+ j = tcaps->nlines;
+ if (tcaps->nlines != tcaps->lpos)
{
- ret = ft_strdup(line);
- line[0] = '\0';
+ while (--j > tcaps->lpos)
+ tputs(tgetstr("up", NULL), 1, ft_putchar);
+ tputs(tgetstr("up", NULL), 1, ft_putchar);
+ tputs(tgetstr("cr", NULL), 1, ft_putchar);
+ while (i < (tcaps->cpos + plen))
+ {
+ tputs(tgetstr("nd", NULL), 1, ft_putchar);
+ i++;
+ }
}
- tcaps->cpos = 0;
- tputs(tgetstr("cr", NULL), 1, ft_putchar);
- if (i < tcaps->nlines && tcaps->nlines != 1)
- while (i++ < tcaps->nlines)
+ else
+ while (len > tcaps->cpos)
{
- write(1, "\n", 1);
+ tputs(tgetstr("le", NULL), 1, ft_putchar);
+ len--;
}
- return (ret);
+ return (len);
}
-char
- *c_insert_char(char *str, char c, t_caps *tcaps)
+unsigned int
+ c_redraw_line(char *line, t_caps *tcaps)
+{
+ unsigned int i;
+ unsigned int j;
+
+ tcaps->nlines = c_get_line_num(line, tcaps->cpos,
+ tcaps->plen, tcaps);
+ i = 0;
+ j = tcaps->lpos;
+ tputs(tgetstr("cr", NULL), 1, ft_putchar);
+ while (--j > 0)
+ {
+ tputs(tgetstr("sf", NULL), 1, ft_putchar);
+ tputs(tgetstr("up", NULL), 1, ft_putchar);
+ tputs(tgetstr("up", NULL), 1, ft_putchar);
+ }
+ while (i++ < tcaps->plen)
+ tputs(tgetstr("nd", NULL), 1, ft_putchar);
+ ft_printf("%s", line);
+ return ((tcaps->cpos != ft_strlen(line)) ?
+ c_redraw_next(tcaps->plen, ft_strlen(line), tcaps) :
+ ft_strlen(line));
+}
+
+void
+ c_add_char(char **line, char *buf, t_caps *tcaps)
{
int i;
size_t j;
@@ -57,21 +86,24 @@ char
i = -1;
j = 0;
- len = (ft_strlen(str) + 2);
- if (!(dst = (char*)malloc((len) * sizeof(char))))
- return (NULL);
+ len = (ft_strlen(*line) + 2);
+ if (!(dst = (char*)ft_calloc(sizeof(char), len)))
+ return ;
while (++i < (int)len)
- {
if (i == (int)tcaps->cpos)
- dst[i] = c;
+ dst[i] = buf[0];
else
- {
- dst[i] = str[j];
- j++;
- }
+ dst[i] = line[0][j++];
+ ft_memdel((void**)&line[0]);
+ *line = ft_strdup(dst);
+ ft_memdel((void**)&dst);
+ tcaps->cpos++;
+ if ((((tcaps->cpos) + tcaps->plen) % tcaps->ws.ws_col) == 0)
+ {
+ write(STDOUT_FILENO, "\n\r", 2);
+ return ;
}
- ft_memdel((void*)&str);
-return (dst);
+ c_redraw_line(*line, tcaps);
}
char
@@ -99,61 +131,20 @@ char
return (dst);
}
-static short
- c_redraw_next(size_t plen, unsigned int len, t_caps *tcaps)
+void
+ c_new_line(t_caps *tcaps)
{
- unsigned int i;
- unsigned int j;
+ size_t i;
- i = (tcaps->nlines == tcaps->lpos) ? 0 :
- tcaps->ws.ws_col * (tcaps->lpos - 1);
- j = tcaps->nlines;
- if (tcaps->nlines != tcaps->lpos)
- {
- while (--j > tcaps->lpos)
- tputs(tgetstr("up", NULL), 1, ft_putchar);
- tputs(tgetstr("up", NULL), 1, ft_putchar);
- tputs(tgetstr("cr", NULL), 1, ft_putchar);
- while (i < (tcaps->cpos + plen))
- {
- tputs(tgetstr("nd", NULL), 1, ft_putchar);
- i++;
- }
- }
- else
- while (len > tcaps->cpos)
+ i = tcaps->lpos;
+ write(1, "\n", 1);
+ tputs(tgetstr("cr", NULL), 1, ft_putchar);
+ c_set_term_raw(0);
+ tcaps->cpos = 0;
+ tputs(tgetstr("cr", NULL), 1, ft_putchar);
+ if (i < tcaps->nlines && tcaps->nlines != 1)
+ while (i++ < tcaps->nlines)
{
- tputs(tgetstr("le", NULL), 1, ft_putchar);
- len--;
+ write(1, "\n", 1);
}
- return (len);
-}
-
-short
- c_redraw_line(char *line, t_caps *tcaps, t_msh *msh)
-{
- unsigned int i;
- unsigned int j;
- size_t len;
-
- (void)msh;
- tcaps->nlines = c_get_line_num(line, tcaps->cpos,
- tcaps->plen, tcaps);
- i = 0;
- j = tcaps->lpos;
- tputs(tgetstr("cr", NULL), 1, ft_putchar);
- while (--j > 0)
- {
- tputs(tgetstr("sf", NULL), 1, ft_putchar);
- tputs(tgetstr("up", NULL), 1, ft_putchar);
- tputs(tgetstr("up", NULL), 1, ft_putchar);
- }
- while (i++ < tcaps->plen)
- tputs(tgetstr("nd", NULL), 1, ft_putchar);
- ft_printf("%s", line);
- len = ft_strlen(line);
- /* ft_memdel((void*)&line); */
- return ((tcaps->cpos != len) ?
- c_redraw_next(tcaps->plen, len, tcaps) :
- len);
}