feat: base + display functions!!!!!

This commit is contained in:
mcolonna 2024-02-26 14:18:51 +01:00
parent ebb5931632
commit 664b18eb2e
187 changed files with 29451 additions and 0 deletions

37
libtf/src/data.c Normal file
View file

@ -0,0 +1,37 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* data.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/09 17:36:23 by mcolonna #+# #+# */
/* Updated: 2024/02/13 17:03:23 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#include <stddef.h>
void data_cpy(void *dest, const void *src, size_t size)
{
unsigned int i;
if (dest < src)
{
i = 0;
while (i < size)
{
((char *)dest)[i] = ((char *)src)[i];
i++;
}
}
else
{
i = size;
while (i > 0)
{
i--;
((char *)dest)[i] = ((char *)src)[i];
}
}
}

33
libtf/src/error.c Normal file
View file

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* error.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/06 18:47:37 by mcolonna #+# #+# */
/* Updated: 2024/02/06 19:08:01 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#include "libtf.h"
static t_const_string pop_msg(t_const_string msg)
{
static t_const_string value = NULL;
t_const_string tmp;
tmp = value;
value = msg;
return (tmp);
}
void err_remember(t_const_string msg)
{
pop_msg(msg);
}
t_const_string err_get(void)
{
return (pop_msg(NULL));
}

102
libtf/src/list1.c Normal file
View file

@ -0,0 +1,102 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* list1.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/10 15:36:25 by mcolonna #+# #+# */
/* Updated: 2024/02/13 17:02:25 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#include "libtf.h"
t_list list_createempty(t_memclass mc)
{
t_list r;
r.first = NULL;
r.last = NULL;
r.size = 0;
r.mc = mc;
return (r);
}
void list_add(t_err *err, t_list *list, void *value)
{
t_list_element *new_element;
new_element = mem_alloc(err, list->mc, sizeof(t_list_element));
if (!new_element)
return ;
new_element->value = value;
new_element->next = NULL;
new_element->previous = list->last;
if (list->last)
{
list->last->next = new_element;
list->last = new_element;
}
else
{
list->first = new_element;
list->last = new_element;
}
list->size++;
}
void list_revadd(t_err *err, t_list *list, void *value)
{
t_list_element *new_element;
new_element = mem_alloc(err, list->mc, sizeof(t_list_element));
if (!new_element)
return ;
new_element->value = value;
new_element->next = list->first;
new_element->previous = NULL;
if (list->first)
{
list->first->previous = new_element;
list->first = new_element;
}
else
{
list->first = new_element;
list->last = new_element;
}
list->size++;
}
void *list_get(t_err *err, const t_list *list, int index)
{
t_list_element *el;
el = list->first;
if (index < 0)
return ((*err)("index error"), NULL);
while (el && index)
{
el = el->next;
index--;
}
if (!el)
return ((*err)("index error"), NULL);
return (el->value);
}
void *list_revget(t_err *err, t_list list, int index)
{
t_list_element *el;
el = list.last;
while (el && index)
{
el = el->previous;
index--;
}
if (!el)
return ((*err)("index error"), NULL);
return (el->value);
}

86
libtf/src/list2.c Normal file
View file

@ -0,0 +1,86 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* list2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/15 15:58:05 by mcolonna #+# #+# */
/* Updated: 2024/02/13 17:02:42 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#include "libtf.h"
void *list_pop(t_err *err, t_list *list)
{
t_list_element *r;
void *rr;
if (!list->first)
return ((*err)("empty list"), NULL);
r = list->first;
list->first = r->next;
if (!list->first)
list->last = NULL;
else
list->first->previous = NULL;
list->size--;
rr = r->value;
mem_free(r);
return (rr);
}
void *list_revpop(t_err *err, t_list *list)
{
t_list_element *r;
void *rr;
if (!list->last)
return ((*err)("empty list"), NULL);
r = list->last;
list->last = r->previous;
if (!list->last)
list->first = NULL;
else
list->first->next = NULL;
list->size--;
rr = r->value;
mem_free(r);
return (rr);
}
int list_getsize(const t_list *list)
{
return (list->size);
}
void list_rotate(t_list *list)
{
t_list_element *el;
if (list->size == 0)
return ;
el = list->first;
list->first = el->next;
list->last->next = el;
el->previous = list->last;
el->next->previous = NULL;
el->next = NULL;
list->last = el;
}
void list_revrotate(t_list *list)
{
t_list_element *el;
if (list->size == 0)
return ;
el = list->last;
list->last = el->previous;
list->first->previous = el;
el->next = list->first;
el->previous->next = NULL;
el->previous = NULL;
list->first = el;
}

84
libtf/src/mem.c Normal file
View file

@ -0,0 +1,84 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* mem.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/18 11:08:16 by mcolonna #+# #+# */
/* Updated: 2024/02/23 13:27:46 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#include "libtf.h"
#include "mem_utils.h"
#include <stdlib.h>
t_memclass mem_newclass(t_err *err)
{
t_memclass_in r;
r = malloc(sizeof(t_memclass_in));
if (!r)
return ((*err)("alloc error"), NULL);
r->first = NULL;
return (r);
}
void *mem_alloc(t_err *err, t_memclass mc, size_t size)
{
void *r;
t_element *new_element;
t_memclass_in mc_in;
mc_in = (t_memclass_in) mc;
new_element = malloc(sizeof(t_element));
if (!new_element)
return ((*err)("alloc error"), NULL);
r = create_address_with_element(new_element, size);
if (!r)
return ((*err)("alloc error"), NULL);
new_element->address = r;
new_element->next = mc_in->first;
if (new_element->next)
new_element->next->previous = new_element;
new_element->previous = NULL;
new_element->mc = mc;
mc_in->first = new_element;
return (r);
}
void mem_freeall(t_memclass mc)
{
t_element *el;
t_element *next;
if (!mc)
return ;
el = ((t_memclass_in) mc)->first;
while (el)
{
next = el->next;
free_address_with_element(el->address);
free(el);
el = next;
}
free(mc);
}
void mem_free(void *address)
{
t_element *el;
if (!address)
return ;
el = get_address_element(address);
free_address_with_element(address);
if (el->previous)
el->previous->next = el->next;
else
el->mc->first = el->next;
if (el->next)
el->next->previous = el->previous;
free(el);
}

34
libtf/src/mem_utils.c Normal file
View file

@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* mem_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/18 16:36:49 by mcolonna #+# #+# */
/* Updated: 2024/01/23 18:16:18 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#include "mem_utils.h"
void *create_address_with_element(t_element *element, size_t size)
{
void *r;
r = malloc(sizeof(t_element *) + size);
if (!r)
return (free(r), NULL);
*((t_element **)r) = element;
return ((void *)((char *)r + sizeof(t_element *)));
}
t_element *get_address_element(void *address)
{
return (*(t_element **)((char *)address - sizeof(t_element *)));
}
void free_address_with_element(void *address)
{
free((char *)address - sizeof(t_element *));
}

27
libtf/src/print.c Normal file
View file

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* print.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/08 14:38:27 by mcolonna #+# #+# */
/* Updated: 2024/02/20 17:07:33 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#include "libtf.h"
#include <unistd.h>
void print_str(t_err *err, int fd, t_const_string str)
{
if (write(fd, str, str_len(str)) == -1)
(*err)("errno");
}
void print_line(t_err *err, int fd, t_const_string str)
{
print_str(err, fd, str);
if (write(fd, "\n", 1) == -1)
(*err)("errno");
}

104
libtf/src/read_line.c Normal file
View file

@ -0,0 +1,104 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* read_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/30 15:39:50 by mcolonna #+# #+# */
/* Updated: 2024/02/23 14:16:55 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
#include "libtf.h"
#include "read_line_utils.h"
// If *eol is set to:
// - 1, that means the line is ended with a '\n'.
// - 0, no '\n' found, buf is now empty.
// If error, returns -1 or else returns 1.
static int pop_until_line_break(int *eol, t_string2 *line, t_buf *buf)
{
int i1;
int i2;
*eol = 0;
i1 = 0;
while (!*eol && i1 < buf->size)
{
if (buf->str[i1] == '\n')
*eol = 1;
i1++;
}
if (append_to_string(line, buf->str, i1) == -1)
return (-1);
i2 = 0;
while (i2 < buf->size - i1)
{
buf->str[i2] = buf->str[i2 + i1];
i2++;
}
buf->size -= i1;
return (1);
}
// Returns -1 if error, or else 1.
static int fill_buf(int *eof, t_buf *buf, int fd)
{
int r;
const t_errmc errmc = setget_errmc(NULL, NULL);
*eof = 0;
r = read(fd, buf->str, BUFFER_SIZE);
if (r == -1)
return (errmc.err("errno"), -1);
buf->size += r;
if (!r)
*eof = 1;
return (1);
}
// Free things and returns to_return.
static void *free_and_return(
void *to_return, t_buf **buf, t_string2 **string, void *any)
{
if (buf)
{
mem_free(*buf);
*buf = NULL;
}
if (string)
free_string(string);
mem_free(any);
return (to_return);
}
t_const_string read_line(t_err err, t_memclass mc, int fd)
{
static t_buf *bufs[NB_FILE_DESCRIPTORS];
t_string2 *line;
int eofeol[2];
setget_errmc(err, mc);
if (fd >= NB_FILE_DESCRIPTORS || fd < 0)
return (err("bad value"), NULL);
if (!bufs[fd] && !create_empty_buf(&bufs[fd]))
return (NULL);
if (!create_empty_string(&line))
return (free_and_return(NULL, &bufs[fd], NULL, NULL));
eofeol[0] = 0;
eofeol[1] = 0;
while (!eofeol[0])
{
if (pop_until_line_break(&eofeol[1], line, bufs[fd]) == -1)
return (free_and_return(NULL, &bufs[fd], &line, NULL));
if (eofeol[1])
break ;
if (fill_buf(&eofeol[0], bufs[fd], fd) == -1)
return (free_and_return(NULL, &bufs[fd], &line, NULL));
}
if (append_to_string(line, "", 1) == 1 && line->size > 1)
return (free_and_return(line->str, NULL, NULL, line));
return (free_and_return(NULL, &bufs[fd], &line, NULL));
}

View file

@ -0,0 +1,87 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* read_line_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/06 14:28:19 by mcolonna #+# #+# */
/* Updated: 2024/02/23 14:18:00 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#include "libtf.h"
#include "read_line_utils.h"
int create_empty_buf(t_buf **result)
{
const t_errmc errmc = setget_errmc(NULL, NULL);
*result = mem_alloc(errmc.err, errmc.mc, sizeof(t_buf));
if (!*result)
return (0);
(*result)->size = 0;
return (1);
}
int create_empty_string(t_string2 **result)
{
const t_errmc errmc = setget_errmc(NULL, NULL);
*result = mem_alloc(errmc.err, errmc.mc, sizeof(t_string2));
if (!*result)
return (0);
(*result)->str = mem_alloc(errmc.err, errmc.mc, sizeof(char));
if (!(*result)->str)
{
mem_free(*result);
return (0);
}
(*result)->size = 0;
return (1);
}
int append_to_string(t_string2 *string, char *to_append, int to_append_size)
{
char *new_str;
int i;
const t_errmc errmc = setget_errmc(NULL, NULL);
new_str = mem_alloc(errmc.err, errmc.mc,
(string->size + to_append_size) * sizeof(char));
if (!new_str)
return (-1);
i = -1;
while (++i < string->size)
new_str[i] = string->str[i];
i = -1;
while (++i < to_append_size)
new_str[i + string->size] = to_append[i];
mem_free(string->str);
string->str = new_str;
string->size += to_append_size;
return (1);
}
void free_string(t_string2 **string)
{
mem_free((*string)->str);
mem_free(*string);
*string = NULL;
}
t_errmc setget_errmc(t_err err, t_memclass mc)
{
static t_err *err_v = NULL;
static t_memclass mc_v = NULL;
t_errmc r;
if (err && mc)
{
err_v = err;
mc_v = mc;
}
r.err = err_v;
r.mc = mc_v;
return (r);
}

42
libtf/src/str1.c Normal file
View file

@ -0,0 +1,42 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* str1.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/22 13:39:04 by mcolonna #+# #+# */
/* Updated: 2024/02/15 15:28:25 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#include "libtf.h"
#include "str_utils.h"
int str_len(t_const_string str)
{
int i;
i = 0;
while (str[i])
i++;
return (i);
}
t_string str_dup(t_err err, t_memclass mc, t_const_string str)
{
t_string r;
int i;
r = mem_alloc(err, mc, str_len(str) * sizeof(char));
if (!r)
return (r);
i = 0;
while (str[i])
{
r[i] = str[i];
i++;
}
r[i] = '\0';
return (r);
}

89
libtf/src/str2.c Normal file
View file

@ -0,0 +1,89 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* str2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/30 15:19:37 by mcolonna #+# #+# */
/* Updated: 2024/02/15 11:12:22 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#include "libtf.h"
unsigned int str_touint(t_err err, t_const_string str)
{
t_stream stream;
unsigned int r;
streamstr_init(&stream, str);
r = stream_readuint(&err_remember, &stream);
if (err_get() || stream_read(&stream))
return ((*err)("bad value"), 0);
return (r);
}
int str_toint(t_err err, t_const_string str)
{
t_stream stream;
int r;
streamstr_init(&stream, str);
r = stream_readint(&err_remember, &stream);
if (err_get() || stream_read(&stream))
return ((*err)("bad value"), -1);
return (r);
}
t_string str_join(t_err *err, t_memclass mc, t_const_string str1,
t_const_string str2)
{
const size_t len1 = str_len(str1);
const size_t len2 = str_len(str2);
t_string r;
r = mem_alloc(err, mc, sizeof(char) * (len1 + len2 + 1));
if (!r)
return (NULL);
data_cpy(r, str1, len1);
data_cpy(r + len1, str2, len2);
r[len1 + len2] = '\0';
return (r);
}
t_string str_uinttostr(t_err *err, t_memclass mc, unsigned int n)
{
const t_const_string digits[] = {
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
t_string r;
t_string tmp;
if (n == 0)
return (str_dup(err, mc, "0"));
r = str_dup(err, mc, digits[n % 10]);
n /= 10;
while (n > 0)
{
tmp = r;
r = str_join(err, mc, digits[n % 10], r);
mem_free(tmp);
n /= 10;
}
return (r);
}
t_string str_inttostr(t_err *err, t_memclass mc, int n)
{
t_string r;
if (n >= 0)
return (str_uinttostr(err, mc, n));
else
{
r = str_uinttostr(err, mc, -n);
if (!r)
return (NULL);
return (str_join(err, mc, "-", r));
}
}

105
libtf/src/str3.c Normal file
View file

@ -0,0 +1,105 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* str3.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/22 13:39:04 by mcolonna #+# #+# */
/* Updated: 2024/02/23 11:36:07 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#include "libtf.h"
#include "str_utils.h"
static t_two_strings two_strings(t_const_string str, t_const_string charset)
{
t_two_strings r;
r.str = str;
r.charset = charset;
return (r);
}
static t_string get_word_at_index(
t_err *err, t_memclass mc, int index, t_two_strings args)
{
int i;
int j;
t_string r;
i = 0;
while (char_isin(args.str[i], args.charset))
i++;
while (--index >= 0)
{
while (args.str[i] && !char_isin(args.str[i], args.charset))
i++;
while (char_isin(args.str[i], args.charset))
i++;
}
j = 0;
while (args.str[i + j] && !char_isin(args.str[i + j], args.charset))
j++;
r = mem_alloc(err, mc, (j + 1) * sizeof(char));
if (!r)
return (NULL);
r[j] = '\0';
while (--j >= 0)
r[j] = args.str[i + j];
return (r);
}
static int count_words(t_const_string str, t_const_string charset)
{
t_stream stream;
int r;
streamstr_init(&stream, str);
r = 0;
while (stream_read(&stream))
{
stream_skip(&stream, charset);
if (stream_read(&stream))
{
r++;
while (stream_read(&stream)
&& !char_isin(stream_read(&stream), charset))
stream_pop(&stream);
}
}
return (r);
}
t_string *str_split(
t_err err, t_memclass mc, t_const_string str, t_const_string charset)
{
t_stream stream;
t_string *r;
int i;
i = count_words(str, charset);
r = mem_alloc(err, mc, (i + 1) * sizeof(t_string));
if (!r)
return (NULL);
streamstr_init(&stream, str);
r[i] = NULL;
while (--i >= 0)
{
r[i] = get_word_at_index(err, mc, i, two_strings(str, charset));
if (!r[i])
return (NULL);
}
return (r);
}
bool str_eq(t_const_string str1, t_const_string str2)
{
int i;
i = 0;
while (str1[i] && str1[i] == str2[i])
i++;
return (str1[i] == str2[i]);
}

47
libtf/src/str_char1.c Normal file
View file

@ -0,0 +1,47 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* str_char1.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/04 13:09:35 by mcolonna #+# #+# */
/* Updated: 2024/02/13 13:29:59 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#include "libtf.h"
bool char_isin(char c, t_const_string charset)
{
int i;
i = 0;
while (charset[i])
{
if (c == charset[i])
return (true);
i++;
}
return (false);
}
bool char_isdigit(char c)
{
return (c >= '0' && c <= '9');
}
bool char_isupper(char c)
{
return (c >= 'A' && c <= 'Z');
}
bool char_islower(char c)
{
return (c >= 'a' && c <= 'z');
}
bool char_isalpha(char c)
{
return (char_isupper(c) || char_islower(c));
}

18
libtf/src/str_char2.c Normal file
View file

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* str_char2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/04 13:15:23 by mcolonna #+# #+# */
/* Updated: 2024/02/13 13:28:57 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#include "libtf.h"
bool char_isalphanum(char c)
{
return (char_isalpha(c) || char_isdigit(c));
}

23
libtf/src/str_stream.c Normal file
View file

@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* str_stream.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/22 16:17:19 by mcolonna #+# #+# */
/* Updated: 2024/01/23 18:16:52 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#include "libtf.h"
char stream_read(t_stream *stream)
{
return ((*stream->_read)(&stream->_data));
}
char stream_pop(t_stream *stream)
{
return ((*stream->_pop)(&stream->_data));
}

View file

@ -0,0 +1,60 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* str_stream_more.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/04 12:48:47 by mcolonna #+# #+# */
/* Updated: 2024/02/13 17:07:24 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#include "libtf.h"
#include <limits.h>
void stream_skip(t_stream *stream, t_const_string charset)
{
while (stream_read(stream)
&& char_isin(stream_read(stream), charset))
stream_pop(stream);
}
unsigned int stream_readuint(t_err *err, t_stream *stream)
{
unsigned long r;
if (!char_isdigit(stream_read(stream)))
return ((*err)("bad value"), 0);
r = 0;
while (char_isdigit(stream_read(stream)))
{
r *= 10;
r += stream_pop(stream) - '0';
if (r > UINT_MAX)
return ((*err)("bad value"), 0);
}
return ((unsigned int)r);
}
int stream_readint(t_err *err, t_stream *stream)
{
unsigned int abs;
bool negative;
negative = false;
if (char_isin(stream_read(stream), "-+"))
{
if (stream_read(stream) == '-')
negative = true;
stream_pop(stream);
}
abs = stream_readuint(&err_remember, stream);
if (err_get())
return ((*err)("bad value"), -1);
if (negative && -(long)abs >= INT_MIN)
return ((int)-abs);
if (!negative && (long)abs <= INT_MAX)
return ((int)abs);
return ((*err)("bad value"), -1);
}

46
libtf/src/str_streamstr.c Normal file
View file

@ -0,0 +1,46 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* str_streamstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/08 12:09:37 by mcolonna #+# #+# */
/* Updated: 2024/02/12 17:07:35 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#include "libtf.h"
static char streamstr_pop(const void **data)
{
char r;
r = **(char **)data;
if (r)
(*(char **)data)++;
return (r);
}
static char streamstr_read(const void **data)
{
return (**(char **)data);
}
t_stream *streamstr_alloc(t_err err, t_memclass mc, t_const_string str)
{
t_stream *r;
r = mem_alloc(err, mc, sizeof(t_stream));
if (!r)
return (NULL);
streamstr_init(r, str);
return (r);
}
void streamstr_init(t_stream *stream, t_const_string str)
{
stream->_read = &streamstr_read;
stream->_pop = &streamstr_pop;
stream->_data = str;
}