Started from buttom go to the sky
This commit is contained in:
parent
96215449bd
commit
f811e55dea
4781 changed files with 10121 additions and 1743 deletions
74
stdme/src/blx/blx.c
Normal file
74
stdme/src/blx/blx.c
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* blx.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/14 18:01:06 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/31 20:14:31 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/blx/blx.h"
|
||||
#include "me/blx/blx_handlers.h"
|
||||
#include "me/blx/sprite.h"
|
||||
#include "me/blx/inputs.h"
|
||||
#include "me/blx/xdata.h"
|
||||
#include "me/types.h"
|
||||
#include "me/vec/vec_u8.h"
|
||||
#include "me/printf/printf.h"
|
||||
#include <mlx.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
void blx_create_fontsheet(t_blx *app);
|
||||
|
||||
t_blx blx_initialize(t_run_function func, t_free_function free_fn,
|
||||
t_blx_app data)
|
||||
{
|
||||
t_blx ctx;
|
||||
|
||||
ctx = (t_blx){.func = func, .app = data, .free = free_fn};
|
||||
ctx.mlx = mlx_init();
|
||||
if (ctx.mlx == NULL)
|
||||
(me_eprintf("Error:\nfailed to inialize blx (mlx) !\n"), exit(1));
|
||||
ctx.inputs = create_inputs_manager(&ctx);
|
||||
ctx.win = mlx_new_window(ctx.mlx, data.size_x * data.pixel_size, data.size_y
|
||||
* data.pixel_size, data.title);
|
||||
if (!blx_sprite_new(&ctx, data.size_x * data.pixel_size, data.size_y
|
||||
* data.pixel_size, &ctx._data.screen))
|
||||
(me_eprintf("Error:\nfailed to inialize blx (screen) !\n"), exit(1));
|
||||
blx_create_fontsheet(&ctx);
|
||||
return (ctx);
|
||||
}
|
||||
|
||||
//mlx_do_key_autorepeatoff(app.mlx);
|
||||
void blx_run(t_blx app)
|
||||
{
|
||||
mlx_hook(app.win, KEYPRESS, KEYPRESSMASK, &blx_key_pressed_handler, &app);
|
||||
mlx_hook(app.win, KEYRELEASE, KEYRELEASEMASK, &blx_key_released_handler,
|
||||
&app);
|
||||
mlx_hook(app.win, DESTROYNOTIFY, NOEVENTMASK, &blx_key_exit_handler, &app);
|
||||
mlx_loop_hook(app.mlx, &blx_loop_func, &app);
|
||||
mlx_loop(app.mlx);
|
||||
}
|
||||
|
||||
void blx_free(t_blx app)
|
||||
{
|
||||
blx_sprite_free(app._data.screen);
|
||||
blx_sprite_free(app._data.font);
|
||||
mlx_do_key_autorepeaton(app.mlx);
|
||||
if (app.free)
|
||||
app.free(app.app);
|
||||
if (app.win)
|
||||
mlx_destroy_window(app.mlx, app.win);
|
||||
if (app.mlx)
|
||||
{
|
||||
mlx_destroy_display(app.mlx);
|
||||
free(app.mlx);
|
||||
}
|
||||
vec_u8_free(app.inputs.keysyms_held);
|
||||
vec_u8_free(app.inputs.keysyms_pressed);
|
||||
vec_u8_free(app.inputs.keysyms_released);
|
||||
}
|
||||
29
stdme/src/blx/blx_create_fontsheet.c
Normal file
29
stdme/src/blx/blx_create_fontsheet.c
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* blx_create_fontsheet.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/27 19:18:44 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/31 20:10:02 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/blx/blx.h"
|
||||
#include "me/blx/sprite.h"
|
||||
#include "me/types.h"
|
||||
#include "me/printf/printf.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define FONT_SHEET_PATH "textures/font.xpm"
|
||||
|
||||
void blx_create_fontsheet(t_blx *app)
|
||||
{
|
||||
if (!blx_sprite_from_xpm(app, FONT_SHEET_PATH, &app->_data.font))
|
||||
{
|
||||
me_eprintf("Error:\nCouldn't open sprite sheet !\n");
|
||||
exit(5);
|
||||
}
|
||||
}
|
||||
53
stdme/src/blx/blx_handlers.c
Normal file
53
stdme/src/blx/blx_handlers.c
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* blx_handlers.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/21 17:44:55 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/21 19:59:47 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/blx/blx.h"
|
||||
#include "me/blx/blx_handlers.h"
|
||||
#include "me/blx/blx_key.h"
|
||||
#include <mlx.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int blx_key_pressed_handler(t_keysym keysym, t_blx *ctx)
|
||||
{
|
||||
t_usize bit_index;
|
||||
|
||||
bit_index = keysym_to_bit_index(keysym);
|
||||
if (bit_index / 8 >= ctx->inputs.keysyms_pressed.len)
|
||||
return (0);
|
||||
if (is_key_held(ctx, bit_index))
|
||||
return (0);
|
||||
ctx->inputs.keysyms_pressed.buffer[bit_index / 8] |= ((t_u8)1 << (bit_index
|
||||
% 8));
|
||||
return (1);
|
||||
}
|
||||
|
||||
int blx_key_released_handler(t_keysym keysym, t_blx *ctx)
|
||||
{
|
||||
t_usize bit_index;
|
||||
|
||||
bit_index = keysym_to_bit_index(keysym);
|
||||
if (bit_index / 8 >= ctx->inputs.keysyms_released.len)
|
||||
return (0);
|
||||
ctx->inputs.keysyms_held.buffer[bit_index / 8] &= ~((t_u8)1 << (bit_index
|
||||
% 8));
|
||||
ctx->inputs.keysyms_pressed.buffer[bit_index / 8] &= ~((t_u8)1 << (bit_index
|
||||
% 8));
|
||||
ctx->inputs.keysyms_released.buffer[bit_index / 8] |= ((t_u8)1 << (bit_index
|
||||
% 8));
|
||||
return (1);
|
||||
}
|
||||
|
||||
int blx_key_exit_handler(t_blx *app)
|
||||
{
|
||||
app->_data.exit = true;
|
||||
return (1);
|
||||
}
|
||||
50
stdme/src/blx/blx_keycode.c
Normal file
50
stdme/src/blx/blx_keycode.c
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* blx_keysym.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/14 15:07:52 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/14 15:58:17 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/blx/blx.h"
|
||||
#include "me/blx/blx_key.h"
|
||||
#include "me/types.h"
|
||||
#include "me/vec/vec_u8.h"
|
||||
|
||||
t_usize keysym_to_bit_index(t_keysym key)
|
||||
{
|
||||
if (key < 0xff00)
|
||||
return ((((t_usize)key) & 0xff) + 0x0000);
|
||||
if (key >= 0xff00)
|
||||
return ((((t_usize)key) & 0xff) + 0x0100);
|
||||
return (0);
|
||||
}
|
||||
|
||||
bool get_key(t_vec_u8 *key_storage, t_keysym keysym)
|
||||
{
|
||||
t_usize index;
|
||||
|
||||
index = keysym_to_bit_index(keysym);
|
||||
if (index == 0 || index / 8 > key_storage->len)
|
||||
return (false);
|
||||
return (!!(key_storage->buffer[index / 8] & (1 << (index % 8))));
|
||||
}
|
||||
|
||||
bool is_key_pressed(t_blx *ctx, t_keysym key)
|
||||
{
|
||||
return (get_key(&ctx->inputs.keysyms_pressed, key));
|
||||
}
|
||||
|
||||
bool is_key_held(t_blx *ctx, t_keysym key)
|
||||
{
|
||||
return (get_key(&ctx->inputs.keysyms_held, key));
|
||||
}
|
||||
|
||||
bool is_key_released(t_blx *ctx, t_keysym key)
|
||||
{
|
||||
return (get_key(&ctx->inputs.keysyms_released, key));
|
||||
}
|
||||
24
stdme/src/blx/colors.c
Normal file
24
stdme/src/blx/colors.c
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* colors.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/14 16:01:18 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/27 16:26:30 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/blx/colors.h"
|
||||
#include "me/types.h"
|
||||
|
||||
t_color new_color_with_alpha(t_u8 r, t_u8 g, t_u8 b, t_u8 alpha)
|
||||
{
|
||||
return ((t_color){.r = r, .g = g, .b = b, .a = alpha});
|
||||
}
|
||||
|
||||
t_color new_color(t_u8 r, t_u8 g, t_u8 b)
|
||||
{
|
||||
return (new_color_with_alpha(r, g, b, 0x00));
|
||||
}
|
||||
42
stdme/src/blx/draw/draw.c
Normal file
42
stdme/src/blx/draw/draw.c
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* draw.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/21 20:19:59 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/31 15:05:17 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/blx/blx.h"
|
||||
#include "me/blx/sprite.h"
|
||||
#include <mlx.h>
|
||||
#include <stdio.h>
|
||||
|
||||
void blx_clear(t_blx *app, t_color col)
|
||||
{
|
||||
sprite_clear(&app->_data.screen, col);
|
||||
}
|
||||
|
||||
void blx_draw(t_blx *app, t_vi2d pos, t_color col)
|
||||
{
|
||||
t_usize i;
|
||||
t_usize j;
|
||||
|
||||
if (get_draw_mode(app) == MASK && col.a != 0)
|
||||
return ;
|
||||
i = 0;
|
||||
while (i < app->app.pixel_size)
|
||||
{
|
||||
j = 0;
|
||||
while (j < app->app.pixel_size)
|
||||
{
|
||||
sprite_draw(&app->_data.screen, vi2d(pos.x * app->app.pixel_size
|
||||
+ j, pos.y * app->app.pixel_size + i), col);
|
||||
j++;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
}
|
||||
34
stdme/src/blx/draw/draw_sprite.c
Normal file
34
stdme/src/blx/draw/draw_sprite.c
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* draw_sprite.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/26 22:12:31 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/31 15:06:55 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/blx/blx.h"
|
||||
#include "me/blx/sprite.h"
|
||||
#include "me/types.h"
|
||||
|
||||
void draw_sprite(t_blx *app, t_vi2d pos, t_sprite *spr)
|
||||
{
|
||||
t_vi2d p;
|
||||
t_color col;
|
||||
|
||||
p.y = 0;
|
||||
while (p.y < spr->height)
|
||||
{
|
||||
p.x = 0;
|
||||
while (p.x < spr->width)
|
||||
{
|
||||
sprite_get_pixel(spr, p, &col);
|
||||
blx_draw(app, vi2d_add(pos, p), col);
|
||||
p.x++;
|
||||
}
|
||||
p.y++;
|
||||
}
|
||||
}
|
||||
82
stdme/src/blx/draw/draw_string.c
Normal file
82
stdme/src/blx/draw/draw_string.c
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* draw_string.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/27 19:02:59 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/31 15:31:57 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/blx/blx.h"
|
||||
#include "me/blx/colors.h"
|
||||
#include "me/blx/sprite.h"
|
||||
#include "me/types.h"
|
||||
#include "me/vec2/vec2.h"
|
||||
#include <stdio.h>
|
||||
|
||||
static inline void inner(t_blx *app, char c, t_vi2d pixel_pos, t_color col)
|
||||
{
|
||||
t_vi2d o;
|
||||
t_color texel;
|
||||
t_vi2d offset;
|
||||
|
||||
o.x = (c - 32) % 16;
|
||||
o.y = (c - 32) / 16;
|
||||
texel = new_color(0, 0, 0);
|
||||
offset.y = 0;
|
||||
while (offset.y < 8)
|
||||
{
|
||||
offset.x = 0;
|
||||
while (offset.x < 8)
|
||||
{
|
||||
if (!sprite_get_pixel(&app->_data.font, vi2d(offset.x + o.x * 8,
|
||||
offset.y + o.y * 8), &texel) && texel.a == 0)
|
||||
blx_draw(app, vi2d_add(pixel_pos, offset), col);
|
||||
offset.x++;
|
||||
}
|
||||
offset.y++;
|
||||
}
|
||||
}
|
||||
|
||||
static inline t_draw_mode handle_draw_mode(t_blx *app, t_color col)
|
||||
{
|
||||
t_draw_mode m;
|
||||
|
||||
m = get_draw_mode(app);
|
||||
if (col.a != 0x00)
|
||||
set_draw_mode(app, ALPHA);
|
||||
else
|
||||
set_draw_mode(app, MASK);
|
||||
return (m);
|
||||
}
|
||||
|
||||
void blx_draw_string(t_blx *app, t_vi2d pos, t_const_str sText, t_color col)
|
||||
{
|
||||
t_vi2d s;
|
||||
t_draw_mode m;
|
||||
char c;
|
||||
|
||||
s.x = 0;
|
||||
s.y = 0;
|
||||
m = handle_draw_mode(app, col);
|
||||
while (*sText)
|
||||
{
|
||||
c = *sText++;
|
||||
if (c == '\n')
|
||||
{
|
||||
s.x = 0;
|
||||
s.y += 8;
|
||||
}
|
||||
else if (c == '\t')
|
||||
s.x += 8 * 4;
|
||||
else
|
||||
{
|
||||
inner(app, c, vi2d(pos.x + s.x, pos.y + s.y), col);
|
||||
s.x += 8;
|
||||
}
|
||||
}
|
||||
set_draw_mode(app, m);
|
||||
}
|
||||
40
stdme/src/blx/inputs.c
Normal file
40
stdme/src/blx/inputs.c
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* inputs.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/15 16:38:08 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/30 18:15:42 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/blx/blx.h"
|
||||
#include "me/blx/inputs.h"
|
||||
#include "me/vec/vec_u8.h"
|
||||
#include <stdio.h>
|
||||
|
||||
static t_vec_u8 alloc_input_vector(void)
|
||||
{
|
||||
t_vec_u8 out;
|
||||
|
||||
out = vec_u8_new(64, NULL);
|
||||
while (out.len < 64)
|
||||
vec_u8_push(&out, 0);
|
||||
return (out);
|
||||
}
|
||||
|
||||
t_blx_input create_inputs_manager(t_blx *ctx)
|
||||
{
|
||||
t_blx_input out;
|
||||
|
||||
(void)(ctx);
|
||||
out = (t_blx_input){
|
||||
.mouse = 0,
|
||||
.keysyms_held = alloc_input_vector(),
|
||||
.keysyms_pressed = alloc_input_vector(),
|
||||
.keysyms_released = alloc_input_vector(),
|
||||
};
|
||||
return (out);
|
||||
}
|
||||
60
stdme/src/blx/logic.c
Normal file
60
stdme/src/blx/logic.c
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* logic.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/15 18:06:06 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/25 18:33:12 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/blx/blx.h"
|
||||
#include "me/blx/blx_key.h"
|
||||
#include "me/blx/sprite.h"
|
||||
#include "me/blx/inputs.h"
|
||||
#include "mlx.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
static void blx_stop(t_blx *app)
|
||||
{
|
||||
mlx_loop_end(app->mlx);
|
||||
blx_free(*app);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
// TODO: implement mouse buttons when needed
|
||||
void blx_post_func(t_blx *ctx)
|
||||
{
|
||||
t_usize idx;
|
||||
|
||||
idx = 0;
|
||||
while (idx < ctx->inputs.keysyms_pressed.len
|
||||
&& idx < ctx->inputs.keysyms_held.len)
|
||||
{
|
||||
ctx->inputs.keysyms_held.buffer[idx] |= \
|
||||
ctx->inputs.keysyms_pressed.buffer[idx];
|
||||
ctx->inputs.keysyms_pressed.buffer[idx] = 0;
|
||||
idx++;
|
||||
}
|
||||
idx = 0;
|
||||
while (idx < ctx->inputs.keysyms_pressed.len)
|
||||
ctx->inputs.keysyms_pressed.buffer[idx++] = 0;
|
||||
idx = 0;
|
||||
while (idx < ctx->inputs.keysyms_released.len)
|
||||
ctx->inputs.keysyms_released.buffer[idx++] = 0;
|
||||
}
|
||||
|
||||
int blx_loop_func(t_blx *ctx)
|
||||
{
|
||||
mlx_put_image_to_window(ctx->mlx, ctx->win, ctx->_data.screen.img, 0, 0);
|
||||
if (ctx->_data.exit || ctx->func(ctx))
|
||||
{
|
||||
blx_stop(ctx);
|
||||
return (0);
|
||||
}
|
||||
blx_post_func(ctx);
|
||||
return (0);
|
||||
}
|
||||
21
stdme/src/blx/sprite/draw_image.c
Normal file
21
stdme/src/blx/sprite/draw_image.c
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* draw_image.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/21 22:43:04 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/31 15:20:24 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/blx/blx.h"
|
||||
#include "me/blx/colors.h"
|
||||
#include "me/blx/sprite.h"
|
||||
#include <mlx.h>
|
||||
|
||||
void blx_draw_sprite_raw(t_blx *ctx, t_vi2d pos, t_sprite *img)
|
||||
{
|
||||
mlx_put_image_to_window(ctx->mlx, ctx->win, img->img, pos.x, pos.y);
|
||||
}
|
||||
78
stdme/src/blx/sprite/draw_pixel_onto.c
Normal file
78
stdme/src/blx/sprite/draw_pixel_onto.c
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* draw_pixel_onto.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/23 16:32:19 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/31 15:21:33 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/blx/blx.h"
|
||||
#include "me/blx/sprite.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
void sprite_draw_other_bpp(t_sprite *spr, t_vi2d pos, t_color col)
|
||||
{
|
||||
(void)(spr);
|
||||
(void)(pos);
|
||||
(void)(col);
|
||||
return ;
|
||||
}
|
||||
|
||||
void sprite_draw(t_sprite *spr, t_vi2d pos, t_color col)
|
||||
{
|
||||
t_u8 *addr;
|
||||
|
||||
if (!(pos.x >= 0 && pos.x < spr->width && pos.y >= 0
|
||||
&& pos.y < spr->height))
|
||||
return ;
|
||||
if (spr->bpp != 32)
|
||||
return (sprite_draw_other_bpp(spr, pos, col));
|
||||
addr = &(spr->data)[pos.y * spr->line_size + pos.x * 4];
|
||||
if (!spr->big_endian)
|
||||
{
|
||||
addr[0] = col.b;
|
||||
addr[1] = col.g;
|
||||
addr[2] = col.r;
|
||||
addr[3] = col.a;
|
||||
}
|
||||
else
|
||||
{
|
||||
addr[0] = col.a;
|
||||
addr[1] = col.r;
|
||||
addr[2] = col.g;
|
||||
addr[3] = col.b;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
addr = &(img->data)[y * img->line_size + (x << 2)];
|
||||
if (!img->big_endian)
|
||||
out = ((unsigned long)addr[0] << 24 | (unsigned long)addr[1] << 16 |
|
||||
(unsigned long)addr[2] << 8 | addr[3]);
|
||||
else
|
||||
out = ((unsigned long)addr[3] << 24 | (unsigned long)addr[2] << 16 |
|
||||
(unsigned long)addr[1] << 8 | addr[0]);
|
||||
return (out);
|
||||
*/
|
||||
|
||||
void sprite_clear(t_sprite *img, t_color col)
|
||||
{
|
||||
t_vi2d pos;
|
||||
|
||||
pos.y = 0;
|
||||
while (pos.y < img->height)
|
||||
{
|
||||
pos.x = 0;
|
||||
while (pos.x < img->width)
|
||||
{
|
||||
sprite_draw(img, pos, col);
|
||||
pos.x++;
|
||||
}
|
||||
pos.y++;
|
||||
}
|
||||
}
|
||||
82
stdme/src/blx/sprite/draw_string.c
Normal file
82
stdme/src/blx/sprite/draw_string.c
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* draw_string.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboxy[1]er <maiboxy[1]er@student.42.fr> +#+ +:+ +#+
|
||||
*/
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/27 18:29:56 by maiboxy[1]er #+# #+# */
|
||||
/* Updated: 2023/12/27 18:43:57 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/blx/blx.h"
|
||||
#include "me/blx/colors.h"
|
||||
#include "me/blx/sprite.h"
|
||||
#include "me/types.h"
|
||||
|
||||
static inline void inner(t_sprite *spr, char c, t_vi2d pixel_pos, t_color col)
|
||||
{
|
||||
t_vi2d o;
|
||||
t_color texel;
|
||||
t_vi2d offset;
|
||||
|
||||
o.x = (c - 32) % 16;
|
||||
o.y = (c - 32) / 16;
|
||||
texel = new_color(0, 0, 0);
|
||||
offset.y = 0;
|
||||
while (offset.y < 8)
|
||||
{
|
||||
offset.x = 0;
|
||||
while (offset.x < 8)
|
||||
{
|
||||
if (!sprite_get_pixel(&spr->ctx->_data.font, vi2d(offset.x + o.x
|
||||
* 8, offset.y + o.y * 8), &texel) && texel.a == 0)
|
||||
sprite_draw(spr, vi2d_add(pixel_pos, offset), col);
|
||||
offset.x++;
|
||||
}
|
||||
offset.y++;
|
||||
}
|
||||
}
|
||||
|
||||
static inline t_draw_mode handle_draw_mode(t_blx *app, t_color col)
|
||||
{
|
||||
t_draw_mode m;
|
||||
|
||||
m = get_draw_mode(app);
|
||||
if (col.a != 0x00)
|
||||
set_draw_mode(app, ALPHA);
|
||||
else
|
||||
set_draw_mode(app, MASK);
|
||||
return (m);
|
||||
}
|
||||
|
||||
void sprite_draw_string(t_sprite *spr, t_vi2d pos, t_const_str sText,
|
||||
t_color col)
|
||||
{
|
||||
t_vi2d s;
|
||||
t_draw_mode m;
|
||||
char c;
|
||||
|
||||
s.x = 0;
|
||||
s.y = 0;
|
||||
m = handle_draw_mode(spr->ctx, col);
|
||||
while (*sText)
|
||||
{
|
||||
c = *sText++;
|
||||
if (c == '\n')
|
||||
{
|
||||
s.x = 0;
|
||||
s.y += 8;
|
||||
}
|
||||
else if (c == '\t')
|
||||
s.x += 8 * 4;
|
||||
else
|
||||
{
|
||||
inner(spr, c, vi2d(pos.x + s.x, pos.y + s.y), col);
|
||||
s.x += 8;
|
||||
}
|
||||
}
|
||||
set_draw_mode(spr->ctx, m);
|
||||
}
|
||||
20
stdme/src/blx/sprite/free_image.c
Normal file
20
stdme/src/blx/sprite/free_image.c
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* free_image.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/21 22:40:12 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/26 15:39:04 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/blx/blx.h"
|
||||
#include "me/blx/sprite.h"
|
||||
#include <mlx.h>
|
||||
|
||||
void blx_sprite_free(t_sprite img)
|
||||
{
|
||||
mlx_destroy_image(img.ctx->mlx, img.img);
|
||||
}
|
||||
51
stdme/src/blx/sprite/get_pixel.c
Normal file
51
stdme/src/blx/sprite/get_pixel.c
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* get_pixel.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/24 00:48:23 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/31 15:27:55 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/blx/colors.h"
|
||||
#include "me/blx/sprite.h"
|
||||
|
||||
static bool sprite_get_pixel_other_bpp(t_sprite *spr, t_vi2d pos, t_color *out)
|
||||
{
|
||||
(void)(spr);
|
||||
(void)(pos);
|
||||
(void)(out);
|
||||
return (true);
|
||||
}
|
||||
|
||||
bool sprite_get_pixel(t_sprite *spr, t_vi2d pos, t_color *out)
|
||||
{
|
||||
t_u8 *addr;
|
||||
t_color col;
|
||||
|
||||
if (!(pos.x >= 0 && pos.x < spr->width && pos.y >= 0
|
||||
&& pos.y < spr->height))
|
||||
return (true);
|
||||
if (spr->bpp != 32)
|
||||
return (sprite_get_pixel_other_bpp(spr, pos, out));
|
||||
addr = &(spr->data)[pos.y * spr->line_size + pos.x * 4];
|
||||
if (spr->big_endian)
|
||||
{
|
||||
col.a = addr[0];
|
||||
col.r = addr[1];
|
||||
col.g = addr[2];
|
||||
col.b = addr[3];
|
||||
}
|
||||
else
|
||||
{
|
||||
col.a = addr[3];
|
||||
col.r = addr[2];
|
||||
col.g = addr[1];
|
||||
col.b = addr[0];
|
||||
}
|
||||
*out = col;
|
||||
return (false);
|
||||
}
|
||||
56
stdme/src/blx/sprite/new_image.c
Normal file
56
stdme/src/blx/sprite/new_image.c
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* new_image.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/21 21:59:04 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/26 15:40:08 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/blx/blx.h"
|
||||
#include "me/blx/colors.h"
|
||||
#include "me/blx/sprite.h"
|
||||
#include <mlx.h>
|
||||
|
||||
bool blx_sprite_from_xpm(t_blx *ctx, t_str path, t_sprite *out)
|
||||
{
|
||||
t_sprite spr;
|
||||
t_i32 bpp;
|
||||
t_i32 size_line;
|
||||
t_i32 endian;
|
||||
|
||||
spr.ctx = ctx;
|
||||
spr.img = mlx_xpm_file_to_image(ctx->mlx, path, &spr.width, &spr.height);
|
||||
if (spr.img == NULL)
|
||||
return (false);
|
||||
spr.data = (t_u8 *)mlx_get_data_addr(spr.img, &bpp, &size_line, &endian);
|
||||
spr.bpp = bpp;
|
||||
spr.line_size = (t_usize)size_line;
|
||||
spr.big_endian = endian == 1;
|
||||
*out = spr;
|
||||
return (true);
|
||||
}
|
||||
|
||||
bool blx_sprite_new(t_blx *ctx, t_i32 width, t_i32 height, t_sprite *out)
|
||||
{
|
||||
t_sprite spr;
|
||||
t_i32 bpp;
|
||||
t_i32 size_line;
|
||||
t_i32 endian;
|
||||
|
||||
spr.ctx = ctx;
|
||||
spr.img = mlx_new_image(ctx->mlx, width, height);
|
||||
if (spr.img == NULL)
|
||||
return (false);
|
||||
spr.data = (t_u8 *)mlx_get_data_addr(spr.img, &bpp, &size_line, &endian);
|
||||
spr.bpp = bpp;
|
||||
spr.line_size = (t_usize)size_line;
|
||||
spr.big_endian = endian == 1;
|
||||
spr.width = width;
|
||||
spr.height = height;
|
||||
*out = spr;
|
||||
return (true);
|
||||
}
|
||||
36
stdme/src/blx/sprite/sprite_draw_onto_sprite.c
Normal file
36
stdme/src/blx/sprite/sprite_draw_onto_sprite.c
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* sprite_draw_onto_sprite.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/26 19:33:22 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/31 15:26:41 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/blx/blx.h"
|
||||
#include "me/blx/colors.h"
|
||||
#include "me/blx/sprite.h"
|
||||
#include "me/types.h"
|
||||
#include "me/vec2/vec2.h"
|
||||
|
||||
void sprite_draw_onto(t_sprite *dest, t_vi2d pos, t_sprite *source)
|
||||
{
|
||||
t_vi2d p;
|
||||
t_color col;
|
||||
|
||||
p.y = 0;
|
||||
while (p.y < source->height)
|
||||
{
|
||||
p.x = 0;
|
||||
while (p.x < source->width)
|
||||
{
|
||||
sprite_get_pixel(source, p, &col);
|
||||
sprite_draw(dest, vi2d_add(pos, p), col);
|
||||
p.x++;
|
||||
}
|
||||
p.y++;
|
||||
}
|
||||
}
|
||||
82
stdme/src/buffered_str/mod.c
Normal file
82
stdme/src/buffered_str/mod.c
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* mod.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/16 17:52:12 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/26 19:54:07 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/buffered_str/buf_str.h"
|
||||
#include "me/mem/mem_alloc.h"
|
||||
#include "me/mem/mem_set_zero.h"
|
||||
#include "me/string/str_l_cat.h"
|
||||
#include "me/string/str_l_copy.h"
|
||||
#include "me/string/str_len.h"
|
||||
#include "me/types.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
bool push_str_buffer(t_buffer_str *buf, t_const_str to_push)
|
||||
{
|
||||
t_usize to_push_len;
|
||||
t_str temp_buffer;
|
||||
t_usize new_capacity;
|
||||
|
||||
if (buf == NULL || to_push == NULL)
|
||||
return (true);
|
||||
to_push_len = str_len(to_push);
|
||||
while (buf->len + to_push_len + 2 > buf->capacity)
|
||||
{
|
||||
new_capacity = (buf->capacity * 3) / 2 + 1;
|
||||
temp_buffer = mem_alloc(new_capacity);
|
||||
if (temp_buffer == NULL)
|
||||
return (true);
|
||||
str_l_copy(temp_buffer, buf->buf, new_capacity);
|
||||
free(buf->buf);
|
||||
buf->buf = temp_buffer;
|
||||
buf->capacity = new_capacity;
|
||||
}
|
||||
buf->len += to_push_len;
|
||||
str_l_cat(buf->buf, to_push, buf->capacity);
|
||||
return (false);
|
||||
}
|
||||
|
||||
bool push_str_char(t_buffer_str *buf, char to_push)
|
||||
{
|
||||
char push_str[2];
|
||||
|
||||
push_str[0] = to_push;
|
||||
push_str[1] = 0;
|
||||
return (push_str_buffer(buf, push_str));
|
||||
}
|
||||
|
||||
void str_clear(t_buffer_str *buf)
|
||||
{
|
||||
mem_set_zero(buf->buf, buf->capacity);
|
||||
buf->len = 0;
|
||||
return ;
|
||||
}
|
||||
|
||||
t_buffer_str alloc_new_buffer(t_usize capacity)
|
||||
{
|
||||
t_buffer_str out;
|
||||
t_str buf;
|
||||
|
||||
if (capacity == 0)
|
||||
capacity = 16;
|
||||
buf = mem_alloc(sizeof(char) * capacity);
|
||||
if (buf == NULL)
|
||||
{
|
||||
out.buf = NULL;
|
||||
out.capacity = 0;
|
||||
out.len = 0;
|
||||
return (out);
|
||||
}
|
||||
out.buf = buf;
|
||||
out.capacity = capacity;
|
||||
out.len = 0;
|
||||
return (out);
|
||||
}
|
||||
20
stdme/src/char/isalnum.c
Normal file
20
stdme/src/char/isalnum.c
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* isalnum.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/04 16:19:40 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/11/09 19:38:51 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/char/isalnum.h"
|
||||
#include "me/char/isalpha.h"
|
||||
#include "me/char/isdigit.h"
|
||||
|
||||
bool me_isalnum(char chr)
|
||||
{
|
||||
return (me_isalpha(chr) || me_isdigit(chr));
|
||||
}
|
||||
18
stdme/src/char/isalpha.c
Normal file
18
stdme/src/char/isalpha.c
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* isalpha.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/04 16:19:40 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/11/08 04:01:25 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/char/isalpha.h"
|
||||
|
||||
bool me_isalpha(char chr)
|
||||
{
|
||||
return (('z' >= chr && chr >= 'a') || ('Z' >= chr && chr >= 'A'));
|
||||
}
|
||||
18
stdme/src/char/isascii.c
Normal file
18
stdme/src/char/isascii.c
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* isascii.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/09 17:51:01 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/04/28 19:41:21 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/char/isascii.h"
|
||||
|
||||
bool me_isascii(char chr)
|
||||
{
|
||||
return (0 <= chr && chr);
|
||||
}
|
||||
18
stdme/src/char/isdigit.c
Normal file
18
stdme/src/char/isdigit.c
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* isdigit.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/04 16:19:40 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/11/04 16:59:06 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/char/isdigit.h"
|
||||
|
||||
bool me_isdigit(char chr)
|
||||
{
|
||||
return (chr >= '0' && chr <= '9');
|
||||
}
|
||||
18
stdme/src/char/islower.c
Normal file
18
stdme/src/char/islower.c
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* islower.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/04 16:19:40 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/11/04 16:58:56 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/char/islower.h"
|
||||
|
||||
bool me_islower(char chr)
|
||||
{
|
||||
return (chr >= 'a' && chr <= 'z');
|
||||
}
|
||||
18
stdme/src/char/isprint.c
Normal file
18
stdme/src/char/isprint.c
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* isprint.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/04 16:19:40 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/11/04 16:42:38 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/char/isprint.h"
|
||||
|
||||
bool me_isprint(char chr)
|
||||
{
|
||||
return (chr >= ' ' && chr <= '~');
|
||||
}
|
||||
19
stdme/src/char/isspace.c
Normal file
19
stdme/src/char/isspace.c
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* isspace.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/06 14:26:25 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/11/08 02:39:24 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/char/isspace.h"
|
||||
|
||||
bool me_isspace(char chr)
|
||||
{
|
||||
return (chr == ' ' || chr == '\f' || chr == '\n' || chr == '\r'
|
||||
|| chr == '\t' || chr == '\v');
|
||||
}
|
||||
18
stdme/src/char/isupper.c
Normal file
18
stdme/src/char/isupper.c
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* isupper.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/04 16:19:40 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/11/08 04:01:59 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/char/isupper.h"
|
||||
|
||||
bool me_isupper(char chr)
|
||||
{
|
||||
return ('Z' >= chr && chr >= 'A');
|
||||
}
|
||||
22
stdme/src/char/tolower.c
Normal file
22
stdme/src/char/tolower.c
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* tolower.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/04 16:47:50 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/11/04 16:56:03 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/char/isupper.h"
|
||||
#include "me/char/tolower.h"
|
||||
|
||||
bool me_tolower(char chr)
|
||||
{
|
||||
if (me_isupper(chr))
|
||||
return (chr + ('a' - 'A'));
|
||||
else
|
||||
return (chr);
|
||||
}
|
||||
22
stdme/src/char/toupper.c
Normal file
22
stdme/src/char/toupper.c
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* toupper.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/04 16:47:50 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/11/04 16:51:28 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/char/islower.h"
|
||||
#include "me/char/toupper.h"
|
||||
|
||||
bool me_toupper(char chr)
|
||||
{
|
||||
if (me_islower(chr))
|
||||
return (chr - ('a' - 'A'));
|
||||
else
|
||||
return (chr);
|
||||
}
|
||||
61
stdme/src/convert/atoi.c
Normal file
61
stdme/src/convert/atoi.c
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* atoi.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/06 14:14:00 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/01/11 15:37:28 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/char/isdigit.h"
|
||||
#include "me/char/isspace.h"
|
||||
#include "me/convert/atoi.h"
|
||||
|
||||
t_i32 me_atoi(t_const_str str)
|
||||
{
|
||||
t_u64 out;
|
||||
t_u64 sign;
|
||||
t_usize i;
|
||||
|
||||
out = 0;
|
||||
i = 0;
|
||||
sign = 1;
|
||||
while (me_isspace(str[i]))
|
||||
i++;
|
||||
if (str[i] == '+' || str[i] == '-')
|
||||
if (str[i++] == '-')
|
||||
sign = -1;
|
||||
while (me_isdigit(str[i]))
|
||||
{
|
||||
out *= 10;
|
||||
out += str[i] - '0';
|
||||
i++;
|
||||
}
|
||||
return ((t_i32)(out * sign));
|
||||
}
|
||||
|
||||
t_i64 me_atoi_64(t_const_str str)
|
||||
{
|
||||
t_u64 out;
|
||||
t_u64 sign;
|
||||
t_usize i;
|
||||
|
||||
out = 0;
|
||||
i = 0;
|
||||
sign = 1;
|
||||
while (me_isspace(str[i]))
|
||||
i++;
|
||||
if (str[i] == '+' || str[i] == '-')
|
||||
if (str[i++] == '-')
|
||||
sign = -1;
|
||||
while (me_isdigit(str[i]))
|
||||
{
|
||||
out *= 10;
|
||||
out += str[i] - '0';
|
||||
i++;
|
||||
}
|
||||
return ((t_i64)(out * sign));
|
||||
}
|
||||
69
stdme/src/convert/itoa.c
Normal file
69
stdme/src/convert/itoa.c
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* itoa.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/08/03 21:05:46 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/11/10 14:56:42 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/convert/itoa.h"
|
||||
#include "me/mem/mem_set.h"
|
||||
#include "me/string/str_clone.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
static void me_itoa_inner(t_u64 nb, t_str out)
|
||||
{
|
||||
t_i32 modulus;
|
||||
bool need_print;
|
||||
char c;
|
||||
t_usize idx;
|
||||
|
||||
need_print = false;
|
||||
modulus = 1000000000;
|
||||
idx = 0;
|
||||
while (modulus)
|
||||
{
|
||||
c = (char)(nb / modulus) + '0';
|
||||
if (c != '0' || need_print)
|
||||
{
|
||||
out[idx++] = c;
|
||||
need_print = true;
|
||||
}
|
||||
nb = nb % modulus;
|
||||
modulus /= 10;
|
||||
}
|
||||
}
|
||||
|
||||
t_str me_itoa(t_i32 nb)
|
||||
{
|
||||
char out[12];
|
||||
t_u64 n;
|
||||
|
||||
n = (t_u64)nb;
|
||||
mem_set(out, 0, 12);
|
||||
if (nb < 0)
|
||||
{
|
||||
out[0] = '-';
|
||||
me_itoa_inner(-n, out + 1);
|
||||
}
|
||||
else if (nb == 0)
|
||||
out[0] = '0';
|
||||
else
|
||||
me_itoa_inner(n, out);
|
||||
return (str_clone(out));
|
||||
}
|
||||
/*R
|
||||
int main(void)
|
||||
{
|
||||
me_putnbr(-2147483648);
|
||||
write(1, "\n", 1);
|
||||
me_putnbr(0);
|
||||
write(1, "\n", 1);
|
||||
me_putnbr(12345);
|
||||
return (0);
|
||||
}
|
||||
R*/
|
||||
27
stdme/src/fs/close.c
Normal file
27
stdme/src/fs/close.c
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* close.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/04 15:56:56 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/10 19:05:48 by maix ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/fs/close.h"
|
||||
#include "me/types.h"
|
||||
#include <unistd.h>
|
||||
|
||||
bool me_close(t_file file, t_i32 *error)
|
||||
{
|
||||
t_i32 res;
|
||||
bool out;
|
||||
|
||||
res = close(file);
|
||||
out = res != 0;
|
||||
if (res && error != NULL)
|
||||
*error = res;
|
||||
return (out);
|
||||
}
|
||||
60
stdme/src/fs/open.c
Normal file
60
stdme/src/fs/open.c
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* open.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/03 15:29:38 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/01/06 18:19:11 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/fs/open.h"
|
||||
#include <fcntl.h>
|
||||
|
||||
t_error me_open(t_const_str path, bool read, bool write, t_file *file_out)
|
||||
{
|
||||
t_file out;
|
||||
int flags;
|
||||
|
||||
flags = 0;
|
||||
if (read && write)
|
||||
flags = O_RDWR;
|
||||
else if (read)
|
||||
flags = O_RDONLY;
|
||||
else if (write)
|
||||
flags = O_WRONLY;
|
||||
out = open(path, flags, 0666);
|
||||
if (out < 0)
|
||||
return (ERROR);
|
||||
*file_out = out;
|
||||
return (NO_ERROR);
|
||||
}
|
||||
|
||||
t_error me_open_truncate(t_const_str path, t_file *file_out)
|
||||
{
|
||||
t_file out;
|
||||
int flags;
|
||||
|
||||
unlink(path);
|
||||
flags = O_WRONLY | O_CREAT | O_TRUNC;
|
||||
out = open(path, flags, 0666);
|
||||
if (out < 0)
|
||||
return (ERROR);
|
||||
*file_out = out;
|
||||
return (NO_ERROR);
|
||||
}
|
||||
|
||||
t_error me_open_create(t_const_str path, t_file *file_out)
|
||||
{
|
||||
t_file out;
|
||||
int flags;
|
||||
|
||||
flags = O_WRONLY | O_CREAT | O_APPEND;
|
||||
out = open(path, flags, 0666);
|
||||
if (out < 0)
|
||||
return (ERROR);
|
||||
*file_out = out;
|
||||
return (NO_ERROR);
|
||||
}
|
||||
20
stdme/src/fs/putchar_fd.c
Normal file
20
stdme/src/fs/putchar_fd.c
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* putchar_fd.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/08 04:42:45 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/11/08 13:22:51 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/fs/putchar_fd.h"
|
||||
#include "me/fs/write.h"
|
||||
#include "me/string/str_len.h"
|
||||
|
||||
void me_putchar_fd(char chr, t_file file)
|
||||
{
|
||||
me_write(file, (t_u8 *)&chr, 1);
|
||||
}
|
||||
23
stdme/src/fs/putendl_fd.c
Normal file
23
stdme/src/fs/putendl_fd.c
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* putendl_fd.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/08 04:42:45 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/11/10 16:23:27 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/fs/putstr_fd.h"
|
||||
#include "me/fs/write.h"
|
||||
#include "me/string/str_len.h"
|
||||
|
||||
void me_putendl_fd(t_str str, t_file file)
|
||||
{
|
||||
if (str == NULL)
|
||||
return ;
|
||||
me_write(file, (t_u8 *)str, str_len(str));
|
||||
me_write(file, (t_u8 *)"\n", 1);
|
||||
}
|
||||
54
stdme/src/fs/putnbr_fd.c
Normal file
54
stdme/src/fs/putnbr_fd.c
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* putnbr_fd.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/08 12:45:06 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/04/28 19:42:10 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/fs/putnbr_fd.h"
|
||||
#include "me/fs/write.h"
|
||||
|
||||
static void me_inner(t_u64 nb, t_str out, t_usize *idx)
|
||||
{
|
||||
bool need_print;
|
||||
t_u64 modulus;
|
||||
char c;
|
||||
|
||||
modulus = 1000000000;
|
||||
need_print = false;
|
||||
while (modulus)
|
||||
{
|
||||
c = (char)(nb / modulus);
|
||||
if (c != 0 || need_print)
|
||||
{
|
||||
out[(*idx)++] = c + '0';
|
||||
need_print = true;
|
||||
}
|
||||
nb = nb % modulus;
|
||||
modulus /= 10;
|
||||
}
|
||||
}
|
||||
|
||||
void me_putnbr_fd(t_i32 n, t_file file)
|
||||
{
|
||||
t_usize idx;
|
||||
t_u64 nb;
|
||||
char out[15];
|
||||
|
||||
nb = (t_u64)n;
|
||||
idx = 0;
|
||||
if (nb < 0)
|
||||
{
|
||||
out[idx++] = '-';
|
||||
nb = -nb;
|
||||
}
|
||||
if (nb == 0)
|
||||
out[idx++] = '0';
|
||||
me_inner(nb, out, &idx);
|
||||
me_write(file, (t_u8 *)out, idx);
|
||||
}
|
||||
22
stdme/src/fs/putstr_fd.c
Normal file
22
stdme/src/fs/putstr_fd.c
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* putstr_fd.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/08 04:42:45 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/11/10 16:23:44 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/fs/putstr_fd.h"
|
||||
#include "me/fs/write.h"
|
||||
#include "me/string/str_len.h"
|
||||
|
||||
void me_putstr_fd(t_str str, t_file file)
|
||||
{
|
||||
if (str == NULL)
|
||||
return ;
|
||||
me_write(file, (t_u8 *)str, str_len(str));
|
||||
}
|
||||
24
stdme/src/fs/read.c
Normal file
24
stdme/src/fs/read.c
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* read.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/03 15:21:19 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/09 18:08:10 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/fs/read.h"
|
||||
#include <unistd.h>
|
||||
|
||||
t_usize me_read(t_file fd, t_u8 *buffer, t_i64 buffer_max, bool *eof_out)
|
||||
{
|
||||
ssize_t out;
|
||||
|
||||
out = read(fd, buffer, buffer_max);
|
||||
if (out == 0 && buffer_max != 0 && eof_out != NULL)
|
||||
*eof_out = true;
|
||||
return (out);
|
||||
}
|
||||
43
stdme/src/fs/read_to_vec.c
Normal file
43
stdme/src/fs/read_to_vec.c
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* read_to_vec.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/24 18:38:47 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/30 18:15:58 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/fs/open.h"
|
||||
#include "me/fs/read.h"
|
||||
#include "me/mem/mem_copy.h"
|
||||
#include "me/vec/vec_u8.h"
|
||||
|
||||
#define READ_BUFFER_SIZE 4096
|
||||
|
||||
bool read_to_vec(t_const_str path, t_vec_u8 *out)
|
||||
{
|
||||
t_u8 temp_buffer[READ_BUFFER_SIZE];
|
||||
t_isize read_amount;
|
||||
t_file f;
|
||||
bool eof;
|
||||
t_usize current_size;
|
||||
|
||||
eof = false;
|
||||
current_size = 0;
|
||||
if (out == NULL || me_open(path, true, false, &f))
|
||||
return (true);
|
||||
*out = vec_u8_new(READ_BUFFER_SIZE, NULL);
|
||||
while (!eof)
|
||||
{
|
||||
read_amount = me_read(f, temp_buffer, READ_BUFFER_SIZE, &eof);
|
||||
if (read_amount < 0)
|
||||
return (true);
|
||||
vec_u8_reserve(out, current_size + (t_usize)read_amount);
|
||||
mem_copy(&out->buffer[out->len], temp_buffer, (t_usize)read_amount);
|
||||
out->len += (t_usize)read_amount;
|
||||
}
|
||||
return (false);
|
||||
}
|
||||
19
stdme/src/fs/write.c
Normal file
19
stdme/src/fs/write.c
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* write.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/03 15:27:33 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/11/03 15:44:38 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/fs/write.h"
|
||||
#include <unistd.h>
|
||||
|
||||
bool me_write(t_file fd, t_u8 *buffer, t_i64 size)
|
||||
{
|
||||
return (write(fd, buffer, size) < 0);
|
||||
}
|
||||
128
stdme/src/gnl/get_next_line.c
Normal file
128
stdme/src/gnl/get_next_line.c
Normal file
|
|
@ -0,0 +1,128 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* get_next_line.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maix <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/23 17:38:21 by maix #+# #+# */
|
||||
/* Updated: 2023/12/11 19:10:26 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/buffered_str/buf_str.h"
|
||||
#include "me/gnl/gnl.h"
|
||||
#include "me/mem/mem_alloc.h"
|
||||
#include "me/mem/mem_move.h"
|
||||
#include "me/string/str_len.h"
|
||||
#include "me/types.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
static t_static_buffer *get_next_line_buffer(t_file fd)
|
||||
{
|
||||
t_usize index;
|
||||
static t_static_buffer bufs[BUFFER_LENGTH] = {0};
|
||||
|
||||
index = 0;
|
||||
while (index < BUFFER_LENGTH && (bufs[index].fd != fd && bufs[index].used))
|
||||
index++;
|
||||
if (index == BUFFER_LENGTH)
|
||||
return (NULL);
|
||||
bufs[index].fd = fd;
|
||||
bufs[index].used = true;
|
||||
return (&bufs[index]);
|
||||
}
|
||||
|
||||
static bool copy_next_line_into_buffer(t_file fd, t_buffer_str *out,
|
||||
char *temp_buffer, t_usize amount)
|
||||
{
|
||||
char *buf;
|
||||
char *newline;
|
||||
bool got_newline;
|
||||
t_usize other_part_len;
|
||||
|
||||
buf = get_next_line_buffer(fd)->buf;
|
||||
newline = buf;
|
||||
while (*newline != '\n' && *newline && newline < buf + amount)
|
||||
newline++;
|
||||
got_newline = *newline == '\n';
|
||||
other_part_len = amount - (t_usize)(newline - buf + !got_newline);
|
||||
if (amount < (t_usize)(newline - buf + !got_newline))
|
||||
other_part_len = 0;
|
||||
mem_move(temp_buffer, buf, (t_usize)(newline - buf + got_newline));
|
||||
buf[(t_usize)(newline - buf)] = 0;
|
||||
temp_buffer[(t_usize)(newline - buf + got_newline)] = 0;
|
||||
mem_move(buf, newline + 1, other_part_len);
|
||||
push_str_buffer(out, temp_buffer);
|
||||
buf[amount - (t_usize)(newline - buf + got_newline)] = 0;
|
||||
return (got_newline);
|
||||
}
|
||||
|
||||
static bool read_and_copy(t_file fd, t_buffer_str *out, char *tmp,
|
||||
t_copy_flags *flags)
|
||||
{
|
||||
t_isize amount;
|
||||
t_static_buffer *buf;
|
||||
|
||||
buf = get_next_line_buffer(fd);
|
||||
amount = read(fd, &buf->buf, BUFFER_SIZE);
|
||||
flags->error = amount < 0;
|
||||
if (flags->error)
|
||||
return (true);
|
||||
buf->init = true;
|
||||
if ((t_isize)amount < (t_isize)BUFFER_SIZE)
|
||||
{
|
||||
copy_next_line_into_buffer(fd, out, tmp, (t_usize)amount);
|
||||
flags->empty_read = (amount == 0 && out->len == 0);
|
||||
return (true);
|
||||
}
|
||||
buf->buf[(t_usize)amount] = 0;
|
||||
return (copy_next_line_into_buffer(fd, out, tmp, (t_usize)amount));
|
||||
}
|
||||
|
||||
static bool handle_leftovers(t_file fd, char *temp_buffer, t_buffer_str *buf)
|
||||
{
|
||||
t_static_buffer *static_buffer;
|
||||
|
||||
static_buffer = get_next_line_buffer(fd);
|
||||
if (static_buffer->init)
|
||||
{
|
||||
if (copy_next_line_into_buffer(fd, buf, temp_buffer,
|
||||
str_len(static_buffer->buf)))
|
||||
{
|
||||
free(temp_buffer);
|
||||
return (true);
|
||||
}
|
||||
}
|
||||
return (false);
|
||||
}
|
||||
|
||||
t_buffer_str get_next_line(t_file fd, bool *error)
|
||||
{
|
||||
t_buffer_str buf;
|
||||
char *temp_buffer;
|
||||
t_copy_flags flags;
|
||||
|
||||
*error = false;
|
||||
if (fd < 0 || BUFFER_SIZE <= 0)
|
||||
return (*error = true, (t_buffer_str){0});
|
||||
flags = (t_copy_flags){
|
||||
.error = false,
|
||||
.empty_read = false,
|
||||
};
|
||||
temp_buffer = mem_alloc(sizeof(char) * (BUFFER_SIZE + 2));
|
||||
buf = alloc_new_buffer(32);
|
||||
if (handle_leftovers(fd, temp_buffer, &buf))
|
||||
return (buf);
|
||||
while (!read_and_copy(fd, &buf, temp_buffer, &flags) && !flags.empty_read)
|
||||
;
|
||||
free(temp_buffer);
|
||||
if (flags.error || flags.empty_read)
|
||||
{
|
||||
free(buf.buf);
|
||||
return (*error = true, (t_buffer_str){0});
|
||||
}
|
||||
return (buf);
|
||||
}
|
||||
39
stdme/src/hash/hash_signed.c
Normal file
39
stdme/src/hash/hash_signed.c
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* hash_signed.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/11 17:26:06 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/11 17:27:12 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/hash/hasher.h"
|
||||
#include "me/types.h"
|
||||
|
||||
void hasher_write_i8(t_hasher *hasher, t_i8 n)
|
||||
{
|
||||
hasher_write_bytes(hasher, (t_u8 *)&n, 1);
|
||||
}
|
||||
|
||||
void hasher_write_i16(t_hasher *hasher, t_i16 n)
|
||||
{
|
||||
hasher_write_bytes(hasher, (t_u8 *)&n, 2);
|
||||
}
|
||||
|
||||
void hasher_write_i32(t_hasher *hasher, t_i32 n)
|
||||
{
|
||||
hasher_write_bytes(hasher, (t_u8 *)&n, 1);
|
||||
}
|
||||
|
||||
void hasher_write_i64(t_hasher *hasher, t_i64 n)
|
||||
{
|
||||
hasher_write_bytes(hasher, (t_u8 *)&n, 1);
|
||||
}
|
||||
|
||||
void hasher_write_isize(t_hasher *hasher, t_isize n)
|
||||
{
|
||||
hasher_write_bytes(hasher, (t_u8 *)&n, sizeof(t_isize));
|
||||
}
|
||||
34
stdme/src/hash/hash_unsigned.c
Normal file
34
stdme/src/hash/hash_unsigned.c
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* hash_unsigned.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/11 17:25:23 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/27 16:37:58 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/hash/hasher.h"
|
||||
#include "me/types.h"
|
||||
|
||||
void hasher_write_u8(t_hasher *hasher, t_u8 byte)
|
||||
{
|
||||
hasher->hash_bytes(hasher->hasher, &byte, 1);
|
||||
}
|
||||
|
||||
void hasher_write_u16(t_hasher *hasher, t_u16 byte)
|
||||
{
|
||||
hasher->hash_bytes(hasher->hasher, (t_u8 *)&byte, 2);
|
||||
}
|
||||
|
||||
void hasher_write_u32(t_hasher *hasher, t_u32 byte)
|
||||
{
|
||||
hasher->hash_bytes(hasher->hasher, (t_u8 *)&byte, 4);
|
||||
}
|
||||
|
||||
void hasher_write_u64(t_hasher *hasher, t_u64 byte)
|
||||
{
|
||||
hasher->hash_bytes(hasher->hasher, (t_u8 *)&byte, 8);
|
||||
}
|
||||
29
stdme/src/hash/hasher.c
Normal file
29
stdme/src/hash/hasher.c
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* hasher.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/11 15:58:12 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/27 16:44:25 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/hash/hasher.h"
|
||||
#include "me/types.h"
|
||||
|
||||
t_u64 hasher_finish(t_hasher *hasher)
|
||||
{
|
||||
return (hasher->finish(hasher->hasher));
|
||||
}
|
||||
|
||||
t_u64 hasher_reset_and_finish(t_hasher *hasher)
|
||||
{
|
||||
return (hasher->reset_and_finish(hasher->hasher));
|
||||
}
|
||||
|
||||
void hasher_write_bytes(t_hasher *hasher, t_u8 *bytes, t_usize count)
|
||||
{
|
||||
hasher->hash_bytes(hasher->hasher, bytes, count);
|
||||
}
|
||||
31
stdme/src/hash/sip/sip13.c
Normal file
31
stdme/src/hash/sip/sip13.c
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* sip13.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/10 19:32:28 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/27 16:48:13 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/hash/sip.h"
|
||||
#include "me/hash/sip/sip_utils.h"
|
||||
#include "me/mem/mem_alloc.h"
|
||||
|
||||
t_hasher hasher_sip13_new(void)
|
||||
{
|
||||
t_hasher out;
|
||||
t_sip13 *inner;
|
||||
|
||||
inner = mem_alloc(sizeof(t_sip13));
|
||||
inner->state = create_state_with_key(0, 0);
|
||||
inner->k0 = 0;
|
||||
inner->k1 = 0;
|
||||
out.hasher = inner;
|
||||
out.hash_bytes = (t_hash_bytes)sip13_write_bytes;
|
||||
out.finish = (t_hasher_finish)sip13_finish;
|
||||
out.reset_and_finish = (t_hasher_reset_and_finish)sip13_reset_and_finish;
|
||||
return (out);
|
||||
}
|
||||
106
stdme/src/hash/sip/sip_utils.c
Normal file
106
stdme/src/hash/sip/sip_utils.c
Normal file
|
|
@ -0,0 +1,106 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* sip_utils.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maix <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/10 20:02:12 by maix #+# #+# */
|
||||
/* Updated: 2023/12/11 19:09:32 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/hash/sip.h"
|
||||
#include "me/hash/sip/sip_utils.h"
|
||||
#include "me/num/u64.h"
|
||||
#include "me/num/usize.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
static t_usize me_min(t_usize lhs, t_usize rhs)
|
||||
{
|
||||
if (lhs < rhs)
|
||||
return (lhs);
|
||||
return (rhs);
|
||||
}
|
||||
|
||||
static t_usize handle_remaining(t_sip13 *self, t_u8 *msg, t_usize count,
|
||||
bool *exit_early)
|
||||
{
|
||||
t_usize needed;
|
||||
|
||||
needed = 0;
|
||||
*exit_early = false;
|
||||
if (self->ntail != 0)
|
||||
{
|
||||
needed = 8 - self->ntail;
|
||||
self->tail |= u64_from_bytes(msg, me_min(count, needed)) << (8 \
|
||||
* self->ntail);
|
||||
if (count < needed)
|
||||
{
|
||||
self->ntail += count;
|
||||
*exit_early = true;
|
||||
return (needed);
|
||||
}
|
||||
else
|
||||
{
|
||||
self->state.v3 ^= self->tail;
|
||||
compress(&self->state);
|
||||
self->state.v0 ^= self->tail;
|
||||
self->ntail = 0;
|
||||
}
|
||||
}
|
||||
return (needed);
|
||||
}
|
||||
|
||||
t_usize read_u8_to_u64(t_u8 p[])
|
||||
{
|
||||
return (((t_u64)((p)[0])) | ((t_u64)((p)[1]) << 8) | \
|
||||
((t_u64)((p)[2]) << 16) | ((t_u64)((p)[3]) << 24) | \
|
||||
((t_u64)((p)[4]) << 32) | ((t_u64)((p)[5]) << 40) | \
|
||||
((t_u64)((p)[6]) << 48) | ((t_u64)((p)[7]) << 56));
|
||||
}
|
||||
|
||||
void sip13_write_bytes(t_sip13 *self, t_u8 *msg, t_usize count)
|
||||
{
|
||||
bool exit_early;
|
||||
t_usize needed;
|
||||
t_usize left;
|
||||
t_usize i;
|
||||
t_u64 mi;
|
||||
|
||||
self->length += count;
|
||||
needed = handle_remaining(self, msg, count, &exit_early);
|
||||
if (exit_early)
|
||||
return ;
|
||||
count = count - needed;
|
||||
left = count & 0x7;
|
||||
i = needed;
|
||||
while (i < count - left)
|
||||
{
|
||||
mi = read_u8_to_u64(msg + i);
|
||||
self->state.v3 ^= mi;
|
||||
compress(&self->state);
|
||||
self->state.v0 ^= mi;
|
||||
i += 8;
|
||||
}
|
||||
self->tail = u64_from_7bytes(msg, i, left);
|
||||
self->ntail = left;
|
||||
}
|
||||
|
||||
t_u64 sip13_finish(t_sip13 *self)
|
||||
{
|
||||
t_sip_state state;
|
||||
t_u64 b;
|
||||
|
||||
state = self->state;
|
||||
b = (((t_u64)self->length & 0xff) << 56) | self->tail;
|
||||
state.v3 ^= b;
|
||||
compress(&state);
|
||||
state.v0 ^= b;
|
||||
state.v2 ^= 0xff;
|
||||
compress(&state);
|
||||
compress(&state);
|
||||
free(self);
|
||||
return (state.v0 ^ state.v1 ^ state.v2 ^ state.v3);
|
||||
}
|
||||
68
stdme/src/hash/sip/sip_utils2.c
Normal file
68
stdme/src/hash/sip/sip_utils2.c
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* sip_utils2.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/11 14:29:03 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/27 16:52:17 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/hash/sip/sip_utils.h"
|
||||
#include "me/num/usize.h"
|
||||
|
||||
void compress(t_sip_state *state)
|
||||
{
|
||||
state->v0 = state->v0 + state->v1;
|
||||
state->v1 = usize_rotate_left(state->v1, 13);
|
||||
state->v1 ^= state->v0;
|
||||
state->v0 = usize_rotate_left(state->v0, 32);
|
||||
state->v2 = state->v2 + state->v3;
|
||||
state->v3 = usize_rotate_left(state->v3, 16);
|
||||
state->v3 ^= state->v2;
|
||||
state->v0 = state->v0 + state->v3;
|
||||
state->v3 = usize_rotate_left(state->v3, 21);
|
||||
state->v3 ^= state->v0;
|
||||
state->v2 = state->v2 + state->v1;
|
||||
state->v1 = usize_rotate_left(state->v1, 17);
|
||||
state->v1 ^= state->v2;
|
||||
state->v2 = usize_rotate_left(state->v2, 32);
|
||||
}
|
||||
|
||||
t_sip_state create_state_with_key(t_u64 k0, t_u64 k1)
|
||||
{
|
||||
t_sip_state state;
|
||||
|
||||
state = (t_sip_state){.v0 = 0, .v1 = 0, .v2 = 0, .v3 = 0};
|
||||
state.v0 = k0 ^ 0x736f6d6570736575;
|
||||
state.v1 = k1 ^ 0x646f72616e646f6d;
|
||||
state.v2 = k0 ^ 0x6c7967656e657261;
|
||||
state.v3 = k1 ^ 0x7465646279746573;
|
||||
return (state);
|
||||
}
|
||||
|
||||
t_u64 sip13_reset_and_finish(t_sip13 *self)
|
||||
{
|
||||
t_sip_state state;
|
||||
t_u64 b;
|
||||
t_u64 ret;
|
||||
|
||||
state = self->state;
|
||||
b = (((t_u64)self->length & 0xff) << 56) | self->tail;
|
||||
state.v3 ^= b;
|
||||
compress(&state);
|
||||
state.v0 ^= b;
|
||||
state.v2 ^= 0xff;
|
||||
compress(&state);
|
||||
compress(&state);
|
||||
ret = (state.v0 ^ state.v1 ^ state.v2 ^ state.v3);
|
||||
self->length = 0;
|
||||
self->state.v0 = self->k0 ^ 0x736f6d6570736575;
|
||||
self->state.v1 = self->k1 ^ 0x646f72616e646f6d;
|
||||
self->state.v2 = self->k0 ^ 0x6c7967656e657261;
|
||||
self->state.v3 = self->k1 ^ 0x7465646279746573;
|
||||
self->ntail = 0;
|
||||
return (ret);
|
||||
}
|
||||
73
stdme/src/img/qoi/qoi_decode.c
Normal file
73
stdme/src/img/qoi/qoi_decode.c
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* qoi_decode.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/25 22:31:40 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/25 22:35:44 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/img/qoi.h"
|
||||
#include "me/img/qoi/qoi_utils.h"
|
||||
#include "me/img/qoi/qoi_decode.h"
|
||||
|
||||
static inline void *qoi_decode_inner(t_decode_vals *vals, const t_u8 *bytes,
|
||||
t_i32 channels, t_i32 size)
|
||||
{
|
||||
t_u8 *pixels;
|
||||
|
||||
pixels = (t_u8 *)mem_alloc(vals->px_len);
|
||||
if (!pixels)
|
||||
return (NULL);
|
||||
mem_set_zero(vals->index, sizeof(vals->index));
|
||||
vals->px.v = 0;
|
||||
vals->px.rgba.a = 255;
|
||||
vals->chunks_len = size - (t_i32) sizeof(t_u8[8]);
|
||||
vals->px_pos = 0;
|
||||
while (vals->px_pos < vals->px_len)
|
||||
{
|
||||
if (vals->run > 0)
|
||||
vals->run--;
|
||||
else if (vals->p < vals->chunks_len)
|
||||
qoi_decode_inner_inner(vals, bytes);
|
||||
pixels[vals->px_pos + 0] = vals->px.rgba.r;
|
||||
pixels[vals->px_pos + 1] = vals->px.rgba.g;
|
||||
pixels[vals->px_pos + 2] = vals->px.rgba.b;
|
||||
if (channels == 4)
|
||||
pixels[vals->px_pos + 3] = vals->px.rgba.a;
|
||||
vals->px_pos += channels;
|
||||
}
|
||||
return (pixels);
|
||||
}
|
||||
|
||||
void *qoi_decode(const void *data, t_i32 size, t_qoi_desc *desc,
|
||||
t_i32 channels)
|
||||
{
|
||||
const t_u8 *bytes;
|
||||
t_decode_vals vals;
|
||||
|
||||
vals.p = 0;
|
||||
vals.run = 0;
|
||||
if (data == NULL || desc == NULL || (channels != 0 && channels != 3
|
||||
&& channels != 4) || size < QOI_HEADER_SIZE
|
||||
+ (t_i32) sizeof(t_u8[8]))
|
||||
return (NULL);
|
||||
bytes = (const t_u8 *)data;
|
||||
vals.header_magic = qoi_read_32(bytes, &vals.p);
|
||||
desc->width = qoi_read_32(bytes, &vals.p);
|
||||
desc->height = qoi_read_32(bytes, &vals.p);
|
||||
desc->channels = bytes[vals.p++];
|
||||
desc->colorspace = bytes[vals.p++];
|
||||
if (desc->width == 0 || desc->height == 0 || desc->channels < 3
|
||||
|| desc->channels > 4 || desc->colorspace > 1
|
||||
|| vals.header_magic != QOI_MAGIC || desc->height >= QOI_PIXELS_MAX
|
||||
/ desc->width)
|
||||
return (NULL);
|
||||
if (channels == 0)
|
||||
channels = desc->channels;
|
||||
vals.px_len = desc->width * desc->height * channels;
|
||||
return (qoi_decode_inner(&vals, bytes, channels, size));
|
||||
}
|
||||
29
stdme/src/img/qoi/qoi_encode.c
Normal file
29
stdme/src/img/qoi/qoi_encode.c
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* qoi_encode.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/25 22:38:42 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/25 22:56:06 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/img/qoi.h"
|
||||
#include "me/img/qoi/qoi_encode.h"
|
||||
#include "me/img/qoi/qoi_utils.h"
|
||||
|
||||
void *qoi_encode(const void *data, const t_qoi_desc *desc, t_i32 *out_len)
|
||||
{
|
||||
t_encode_vals vals;
|
||||
|
||||
if (data == NULL || out_len == NULL || desc == NULL || desc->width == 0
|
||||
|| desc->height == 0 || desc->channels < 3 || desc->channels > 4
|
||||
|| desc->colorspace > 1 || desc->height >= QOI_PIXELS_MAX / desc->width)
|
||||
return (NULL);
|
||||
vals = (t_encode_vals){0};
|
||||
vals.max_size = desc->width * desc->height * (desc->channels + 1)
|
||||
+ QOI_HEADER_SIZE + sizeof(t_u8[8]);
|
||||
return (qoi_encode_inner(&vals, desc, (const t_u8 *)data, out_len));
|
||||
}
|
||||
49
stdme/src/img/qoi/qoi_fs.c
Normal file
49
stdme/src/img/qoi/qoi_fs.c
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* qoi_fs.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/24 19:06:05 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/24 19:18:01 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/fs/close.h"
|
||||
#include "me/fs/open.h"
|
||||
#include "me/fs/read_to_vec.h"
|
||||
#include "me/fs/write.h"
|
||||
#include "me/img/qoi.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
t_i32 qoi_write(t_const_str filename, const void *data,
|
||||
const t_qoi_desc *desc)
|
||||
{
|
||||
t_file f;
|
||||
void *encoded;
|
||||
t_i32 size;
|
||||
|
||||
if (me_open(filename, false, true, &f))
|
||||
return (0);
|
||||
encoded = qoi_encode(data, desc, &size);
|
||||
if (!encoded)
|
||||
return (me_close(f, NULL), 0);
|
||||
if (me_write(f, encoded, size))
|
||||
return (me_close(f, NULL), 0);
|
||||
me_close(f, NULL);
|
||||
free(encoded);
|
||||
return (size);
|
||||
}
|
||||
|
||||
void *qoi_read(t_const_str filename, t_qoi_desc *desc, t_i32 channels)
|
||||
{
|
||||
t_vec_u8 out;
|
||||
void *pixels;
|
||||
|
||||
if (read_to_vec(filename, &out))
|
||||
return (NULL);
|
||||
pixels = qoi_decode(out.buffer, out.len, desc, channels);
|
||||
vec_u8_free(out);
|
||||
return (pixels);
|
||||
}
|
||||
36
stdme/src/img/qoi/qoi_utils.c
Normal file
36
stdme/src/img/qoi/qoi_utils.c
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* qoi_utils.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/24 19:02:03 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/24 19:02:42 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/img/qoi/qoi_utils.h"
|
||||
#include "me/types.h"
|
||||
|
||||
void qoi_write_32(t_u8 *bytes, t_i32 *p, t_u32 v)
|
||||
{
|
||||
bytes[(*p)++] = (0xff000000 & v) >> 24;
|
||||
bytes[(*p)++] = (0x00ff0000 & v) >> 16;
|
||||
bytes[(*p)++] = (0x0000ff00 & v) >> 8;
|
||||
bytes[(*p)++] = (0x000000ff & v);
|
||||
}
|
||||
|
||||
t_u32 qoi_read_32(const t_u8 *bytes, t_i32 *p)
|
||||
{
|
||||
t_u32 a;
|
||||
t_u32 b;
|
||||
t_u32 c;
|
||||
t_u32 d;
|
||||
|
||||
a = bytes[(*p)++];
|
||||
b = bytes[(*p)++];
|
||||
c = bytes[(*p)++];
|
||||
d = bytes[(*p)++];
|
||||
return (a << 24 | b << 16 | c << 8 | d);
|
||||
}
|
||||
22
stdme/src/list/list_add_back.c
Normal file
22
stdme/src/list/list_add_back.c
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* list_add_back.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/09 20:38:45 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/09 15:02:42 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/list/list_add_back.h"
|
||||
#include "me/list/list_get_last.h"
|
||||
|
||||
void list_add_back(t_list **list, t_list *new)
|
||||
{
|
||||
if (*list)
|
||||
list_get_last(*list)->next = new;
|
||||
else
|
||||
*list = new;
|
||||
}
|
||||
20
stdme/src/list/list_add_front.c
Normal file
20
stdme/src/list/list_add_front.c
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* list_add_front.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/09 20:15:23 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/09 15:02:50 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/list/list_add_front.h"
|
||||
#include "me/list/list_alloc_node.h"
|
||||
|
||||
void list_add_front(t_list **lst, t_list *new)
|
||||
{
|
||||
new->next = *lst;
|
||||
*lst = new;
|
||||
}
|
||||
26
stdme/src/list/list_alloc_node.c
Normal file
26
stdme/src/list/list_alloc_node.c
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* list_alloc_node.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/09 19:57:28 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/09 18:13:05 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/list/list_alloc_node.h"
|
||||
#include "me/mem/mem_alloc.h"
|
||||
|
||||
t_list *list_alloc_node(void *content)
|
||||
{
|
||||
t_list *out;
|
||||
|
||||
out = mem_alloc(sizeof(t_list) * 1);
|
||||
if (out == NULL)
|
||||
return (NULL);
|
||||
out->content = content;
|
||||
out->next = NULL;
|
||||
return (out);
|
||||
}
|
||||
30
stdme/src/list/list_free_all.c
Normal file
30
stdme/src/list/list_free_all.c
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* list_free_all.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/09 21:35:20 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/09 15:03:34 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/list/list_free_all.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
void list_free_all(t_list **lst, void (*del)(void *))
|
||||
{
|
||||
t_list *tmp;
|
||||
|
||||
if (lst == NULL || del == NULL)
|
||||
return ;
|
||||
while (*lst)
|
||||
{
|
||||
del((*lst)->content);
|
||||
tmp = *lst;
|
||||
*lst = (*lst)->next;
|
||||
free(tmp);
|
||||
}
|
||||
*lst = NULL;
|
||||
}
|
||||
22
stdme/src/list/list_free_one.c
Normal file
22
stdme/src/list/list_free_one.c
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* list_free_one.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/09 21:30:20 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/09 15:03:42 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/list/list_free_one.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
void list_free_one(t_list *lst, void (*del)(void *))
|
||||
{
|
||||
if (lst == NULL || del == NULL)
|
||||
return ;
|
||||
del(lst->content);
|
||||
free(lst);
|
||||
}
|
||||
25
stdme/src/list/list_get_last.c
Normal file
25
stdme/src/list/list_get_last.c
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* list_get_last.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/09 20:37:08 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/09 15:03:49 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/list/list_get_last.h"
|
||||
|
||||
t_list *list_get_last(t_list *list)
|
||||
{
|
||||
t_list *head;
|
||||
|
||||
head = list;
|
||||
if (head == NULL)
|
||||
return (NULL);
|
||||
while (head->next)
|
||||
head = head->next;
|
||||
return (head);
|
||||
}
|
||||
22
stdme/src/list/list_iter.c
Normal file
22
stdme/src/list/list_iter.c
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* list_iter.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/09 21:39:05 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/09 15:03:55 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/list/list_iter.h"
|
||||
|
||||
void list_iter(t_list *list, void (*f)(void *))
|
||||
{
|
||||
while (list)
|
||||
{
|
||||
f(list->content);
|
||||
list = list->next;
|
||||
}
|
||||
}
|
||||
40
stdme/src/list/list_map.c
Normal file
40
stdme/src/list/list_map.c
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* list_map.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/09 21:40:24 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/09 15:05:20 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/list/list_alloc_node.h"
|
||||
#include "me/list/list_free_all.h"
|
||||
#include "me/list/list_map.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
t_list *list_map(t_list *lst, void *(*f)(void *), void (*del)(void *))
|
||||
{
|
||||
void *data;
|
||||
t_list new;
|
||||
t_list *cursor;
|
||||
|
||||
new = (t_list){.next = NULL, .content = NULL};
|
||||
cursor = &new;
|
||||
while (lst)
|
||||
{
|
||||
data = f(lst->content);
|
||||
cursor->next = list_alloc_node(data);
|
||||
if (cursor->next == NULL)
|
||||
{
|
||||
del(data);
|
||||
list_free_all(&new.next, del);
|
||||
return (NULL);
|
||||
}
|
||||
cursor = cursor->next;
|
||||
lst = lst->next;
|
||||
}
|
||||
return (new.next);
|
||||
}
|
||||
28
stdme/src/list/list_size.c
Normal file
28
stdme/src/list/list_size.c
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* list_size.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/09 20:23:19 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/09 15:05:00 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/list/list_size.h"
|
||||
|
||||
t_usize list_size(t_list *lst)
|
||||
{
|
||||
t_list *head;
|
||||
t_usize idx;
|
||||
|
||||
head = lst;
|
||||
idx = 0;
|
||||
while (head)
|
||||
{
|
||||
head = head->next;
|
||||
idx++;
|
||||
}
|
||||
return (idx);
|
||||
}
|
||||
26
stdme/src/mem/mem_alloc.c
Normal file
26
stdme/src/mem/mem_alloc.c
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* mem_alloc.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/06 14:47:49 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/09 18:14:11 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/mem/mem_alloc.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
void *mem_alloc(t_usize size)
|
||||
{
|
||||
void *out;
|
||||
size_t i;
|
||||
|
||||
i = 0;
|
||||
out = malloc(size);
|
||||
while (out && i < size)
|
||||
((t_u8 *)out)[i++] = 0;
|
||||
return (out);
|
||||
}
|
||||
25
stdme/src/mem/mem_alloc_array.c
Normal file
25
stdme/src/mem/mem_alloc_array.c
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* mem_alloc_array.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/06 15:53:21 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/09 18:14:47 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/mem/mem_alloc.h"
|
||||
#include "me/mem/mem_alloc_array.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
void *mem_alloc_array(t_usize item_count, t_usize item_size)
|
||||
{
|
||||
t_usize multiplied;
|
||||
|
||||
multiplied = item_count * item_size;
|
||||
if (multiplied == 0 || multiplied / item_count != item_size)
|
||||
return (NULL);
|
||||
return (mem_alloc(multiplied));
|
||||
}
|
||||
31
stdme/src/mem/mem_compare.c
Normal file
31
stdme/src/mem/mem_compare.c
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* mem_compare.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/06 11:16:02 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/09 15:00:58 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/mem/mem_compare.h"
|
||||
|
||||
t_i32 mem_compare(const void *lhs, const void *rhs, t_usize count)
|
||||
{
|
||||
t_usize i;
|
||||
t_u8 *lhs_;
|
||||
t_u8 *rhs_;
|
||||
|
||||
i = 0;
|
||||
lhs_ = (t_u8 *)lhs;
|
||||
rhs_ = (t_u8 *)rhs;
|
||||
while (i < count)
|
||||
{
|
||||
if (lhs_[i] - rhs_[i])
|
||||
return ((t_i32)(lhs_[i] - rhs_[i]));
|
||||
i++;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
32
stdme/src/mem/mem_copy.c
Normal file
32
stdme/src/mem/mem_copy.c
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* mem_copy.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/06 11:16:02 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/09 15:01:08 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/mem/mem_copy.h"
|
||||
|
||||
void *mem_copy(void *destination, const void *source, t_usize count)
|
||||
{
|
||||
t_usize i;
|
||||
t_u8 *dst;
|
||||
t_u8 *src;
|
||||
|
||||
i = 0;
|
||||
dst = (t_u8 *)destination;
|
||||
src = (t_u8 *)source;
|
||||
if (dst == src)
|
||||
return (destination);
|
||||
while (i < count)
|
||||
{
|
||||
dst[i] = src[i];
|
||||
i++;
|
||||
}
|
||||
return (destination);
|
||||
}
|
||||
29
stdme/src/mem/mem_find.c
Normal file
29
stdme/src/mem/mem_find.c
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* mem_find.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/06 11:16:02 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/09 15:01:36 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/mem/mem_find.h"
|
||||
|
||||
void *mem_find(void *buf, t_u8 find, t_usize count)
|
||||
{
|
||||
t_usize i;
|
||||
t_u8 *buf_bytes;
|
||||
|
||||
i = 0;
|
||||
buf_bytes = (t_u8 *)buf;
|
||||
while (i < count)
|
||||
{
|
||||
if (buf_bytes[i] == find)
|
||||
return ((void *)&buf_bytes[i]);
|
||||
i++;
|
||||
}
|
||||
return (NULL);
|
||||
}
|
||||
38
stdme/src/mem/mem_find_bytes.c
Normal file
38
stdme/src/mem/mem_find_bytes.c
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* mem_find_bytes.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/06 11:16:02 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/01/06 17:14:23 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/mem/mem_find_bytes.h"
|
||||
#include "stdio.h"
|
||||
|
||||
void *mem_find_bytes(void *buf, t_u8 *find, t_usize find_len, t_usize count)
|
||||
{
|
||||
t_usize i;
|
||||
t_usize j;
|
||||
t_u8 *buf_bytes;
|
||||
|
||||
i = 0;
|
||||
buf_bytes = (t_u8 *)buf;
|
||||
while (i < count)
|
||||
{
|
||||
j = 0;
|
||||
while (j < find_len && i + j < count)
|
||||
{
|
||||
if (buf_bytes[i + j] != find[j])
|
||||
break ;
|
||||
j++;
|
||||
}
|
||||
if (j == find_len)
|
||||
return ((void *)&buf_bytes[i]);
|
||||
i++;
|
||||
}
|
||||
return (NULL);
|
||||
}
|
||||
42
stdme/src/mem/mem_move.c
Normal file
42
stdme/src/mem/mem_move.c
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* mem_move.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/06 11:16:02 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/09 15:01:43 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/mem/mem_move.h"
|
||||
|
||||
void *mem_move(void *destination, const void *source, t_usize count)
|
||||
{
|
||||
t_usize i;
|
||||
t_u8 *dst;
|
||||
t_u8 *src;
|
||||
|
||||
i = 0;
|
||||
dst = (t_u8 *)destination;
|
||||
src = (t_u8 *)source;
|
||||
if (dst < src)
|
||||
{
|
||||
while (i < count)
|
||||
{
|
||||
dst[i] = src[i];
|
||||
i++;
|
||||
}
|
||||
}
|
||||
else if (dst > src)
|
||||
{
|
||||
i = count;
|
||||
while (i > 0)
|
||||
{
|
||||
i--;
|
||||
dst[i] = src[i];
|
||||
}
|
||||
}
|
||||
return (destination);
|
||||
}
|
||||
27
stdme/src/mem/mem_set.c
Normal file
27
stdme/src/mem/mem_set.c
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* mem_set.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/06 11:16:02 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/09 18:15:22 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/mem/mem_set.h"
|
||||
|
||||
void mem_set(void *buf, t_u8 fill_by, t_usize count)
|
||||
{
|
||||
t_usize i;
|
||||
t_u8 *buf_bytes;
|
||||
|
||||
i = 0;
|
||||
buf_bytes = (t_u8 *)buf;
|
||||
while (i < count)
|
||||
{
|
||||
buf_bytes[i] = fill_by;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
27
stdme/src/mem/mem_set_zero.c
Normal file
27
stdme/src/mem/mem_set_zero.c
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* mem_set_zero.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/06 11:58:11 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/09 15:01:57 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/mem/mem_set_zero.h"
|
||||
|
||||
void mem_set_zero(void *buf, t_usize count)
|
||||
{
|
||||
t_u8 *buffer;
|
||||
t_usize index;
|
||||
|
||||
index = 0;
|
||||
buffer = (t_u8 *)buf;
|
||||
while (index < count)
|
||||
{
|
||||
buffer[index] = 0;
|
||||
index++;
|
||||
}
|
||||
}
|
||||
29
stdme/src/num/u16/rotate.c
Normal file
29
stdme/src/num/u16/rotate.c
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* rotate.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/11 14:08:55 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/11 14:13:32 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/num/u16.h"
|
||||
|
||||
t_u16 u16_rotate_left(t_u16 value, t_usize by)
|
||||
{
|
||||
by &= sizeof(value) * 8 - 1;
|
||||
if (by == 0)
|
||||
return (value);
|
||||
return ((value << by) | (value >> (sizeof(value) * 8 - by)));
|
||||
}
|
||||
|
||||
t_u16 u16_rotate_right(t_u16 value, t_usize by)
|
||||
{
|
||||
by &= sizeof(value) * 8 - 1;
|
||||
if (by == 0)
|
||||
return (value);
|
||||
return ((value >> by) | (value << (sizeof(value) * 8 - by)));
|
||||
}
|
||||
29
stdme/src/num/u32/rotate.c
Normal file
29
stdme/src/num/u32/rotate.c
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* rotate.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/11 14:08:55 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/11 14:13:32 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/num/u32.h"
|
||||
|
||||
t_u32 u32_rotate_left(t_u32 value, t_usize by)
|
||||
{
|
||||
by &= sizeof(value) * 8 - 1;
|
||||
if (by == 0)
|
||||
return (value);
|
||||
return ((value << by) | (value >> (sizeof(value) * 8 - by)));
|
||||
}
|
||||
|
||||
t_u32 u32_rotate_right(t_u32 value, t_usize by)
|
||||
{
|
||||
by &= sizeof(value) * 8 - 1;
|
||||
if (by == 0)
|
||||
return (value);
|
||||
return ((value >> by) | (value << (sizeof(value) * 8 - by)));
|
||||
}
|
||||
57
stdme/src/num/u64/from_bytes.c
Normal file
57
stdme/src/num/u64/from_bytes.c
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* from_bytes.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/11 14:30:56 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/11 19:09:52 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/num/u64.h"
|
||||
|
||||
t_u64 u64_from_7bytes(t_u8 *msg, t_usize start, t_usize len)
|
||||
{
|
||||
t_usize i;
|
||||
t_usize out;
|
||||
t_u64 temp_val;
|
||||
|
||||
i = 0;
|
||||
out = 0;
|
||||
if (i + 3 < len)
|
||||
{
|
||||
temp_val = (t_u64)(msg[start + i]);
|
||||
temp_val |= (t_u64)(msg[start + i + 1]) << (8 * 1);
|
||||
temp_val |= (t_u64)(msg[start + i + 2]) << (8 * 2);
|
||||
temp_val |= (t_u64)(msg[start + i + 3]) << (8 * 3);
|
||||
out = temp_val;
|
||||
i += 4;
|
||||
}
|
||||
if (i + 1 < len)
|
||||
{
|
||||
temp_val = ((t_u64)(msg[start + i + 1]) << (8 * 1)) | (t_u64)(msg[start \
|
||||
+ i]);
|
||||
out |= temp_val << (i * 8);
|
||||
i += 2;
|
||||
}
|
||||
if (i++ < len)
|
||||
out |= (t_u64)(msg[start + i - 1]) << ((i - 1) * 8);
|
||||
return (out);
|
||||
}
|
||||
|
||||
t_u64 u64_from_bytes(t_u8 *bytes, t_usize len)
|
||||
{
|
||||
t_u64 out;
|
||||
t_usize i;
|
||||
|
||||
i = 0;
|
||||
out = 0;
|
||||
while (i < len && i < 8)
|
||||
{
|
||||
out |= ((t_u64)(bytes[i])) << (8 * i);
|
||||
i++;
|
||||
}
|
||||
return (out);
|
||||
}
|
||||
29
stdme/src/num/u64/rotate.c
Normal file
29
stdme/src/num/u64/rotate.c
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* rotate.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/11 14:08:55 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/11 14:24:16 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/num/u64.h"
|
||||
|
||||
t_u64 u64_rotate_left(t_u64 value, t_usize by)
|
||||
{
|
||||
by &= sizeof(value) * 8 - 1;
|
||||
if (by == 0)
|
||||
return (value);
|
||||
return ((value << by) | (value >> (sizeof(value) * 8 - by)));
|
||||
}
|
||||
|
||||
t_u64 u64_rotate_right(t_u64 value, t_usize by)
|
||||
{
|
||||
by &= sizeof(value) * 8 - 1;
|
||||
if (by == 0)
|
||||
return (value);
|
||||
return ((value >> by) | (value << (sizeof(value) * 8 - by)));
|
||||
}
|
||||
29
stdme/src/num/u8/rotate.c
Normal file
29
stdme/src/num/u8/rotate.c
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* rotate.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/11 14:08:55 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/11 14:13:32 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/num/u8.h"
|
||||
|
||||
t_u8 u8_rotate_left(t_u8 value, t_usize by)
|
||||
{
|
||||
by &= sizeof(value) * 8 - 1;
|
||||
if (by == 0)
|
||||
return (value);
|
||||
return ((value << by) | (value >> (sizeof(value) * 8 - by)));
|
||||
}
|
||||
|
||||
t_u8 u8_rotate_right(t_u8 value, t_usize by)
|
||||
{
|
||||
by &= sizeof(value) * 8 - 1;
|
||||
if (by == 0)
|
||||
return (value);
|
||||
return ((value >> by) | (value << (sizeof(value) * 8 - by)));
|
||||
}
|
||||
29
stdme/src/num/usize/rotate.c
Normal file
29
stdme/src/num/usize/rotate.c
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* rotate.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/11 14:08:55 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/11 14:13:32 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/num/usize.h"
|
||||
|
||||
t_usize usize_rotate_left(t_usize value, t_usize by)
|
||||
{
|
||||
by &= sizeof(value) * 8 - 1;
|
||||
if (by == 0)
|
||||
return (value);
|
||||
return ((value << by) | (value >> (sizeof(value) * 8 - by)));
|
||||
}
|
||||
|
||||
t_usize usize_rotate_right(t_usize value, t_usize by)
|
||||
{
|
||||
by &= sizeof(value) * 8 - 1;
|
||||
if (by == 0)
|
||||
return (value);
|
||||
return ((value >> by) | (value << (sizeof(value) * 8 - by)));
|
||||
}
|
||||
70
stdme/src/printf/formatter/char.c
Normal file
70
stdme/src/printf/formatter/char.c
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* char.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/18 18:12:11 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/11 19:18:48 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/mem/mem_set.h"
|
||||
#include "me/printf/formatter/utils.h"
|
||||
#include "me/printf/printf.h"
|
||||
#include "me/string/str_len.h"
|
||||
#include "me/string/str_substring.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
void printf_c(t_printf_arg data, t_printf_func f)
|
||||
{
|
||||
char value[2];
|
||||
t_str start_num;
|
||||
|
||||
value[0] = *(char *)data.argument;
|
||||
value[1] = 0;
|
||||
start_num = &value[0];
|
||||
pad_and_stuff(
|
||||
(t_pad_and_stuff_args){\
|
||||
.fill_zero = 0, .fill = 0, .len = 1, .pretty = "", .pretty_len = 0, \
|
||||
.str = start_num, .allow_zero_fill = false, .sign = NULL, \
|
||||
.sign_len = 0, }, data, f);
|
||||
}
|
||||
|
||||
t_usize printf_s_inner(t_str *value, t_printf_arg *data, t_printf_func *f)
|
||||
{
|
||||
t_usize len;
|
||||
|
||||
if (*value == NULL)
|
||||
{
|
||||
if (data->flags & (PRECISION) && data->extra.precision < 6)
|
||||
*value = "";
|
||||
else
|
||||
*value = "(null)";
|
||||
}
|
||||
len = str_len(*value);
|
||||
if (data->flags & (PRECISION) && len > data->extra.precision)
|
||||
len = data->extra.precision;
|
||||
if (data->flags & (PRECISION) && len < data->extra.precision)
|
||||
data->extra.precision = len;
|
||||
(void)(f);
|
||||
return (len);
|
||||
}
|
||||
|
||||
void printf_s(t_printf_arg data, t_printf_func f)
|
||||
{
|
||||
t_str value;
|
||||
t_str start_num;
|
||||
t_usize len;
|
||||
|
||||
value = (t_str)data.argument;
|
||||
len = printf_s_inner(&value, &data, &f);
|
||||
start_num = str_substring(value, 0, len);
|
||||
pad_and_stuff(
|
||||
(t_pad_and_stuff_args){\
|
||||
.fill_zero = 0, .fill = 0, .len = len, .pretty = "", .pretty_len = 0, \
|
||||
.str = start_num, .allow_zero_fill = false, .sign = NULL, \
|
||||
.sign_len = 0, }, data, f);
|
||||
free(start_num);
|
||||
}
|
||||
89
stdme/src/printf/formatter/decimal.c
Normal file
89
stdme/src/printf/formatter/decimal.c
Normal file
|
|
@ -0,0 +1,89 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* decimal.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maix <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/18 01:44:35 by maix #+# #+# */
|
||||
/* Updated: 2023/12/11 19:19:27 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/mem/mem_alloc_array.h"
|
||||
#include "me/mem/mem_set.h"
|
||||
#include "me/printf/formatter/utils.h"
|
||||
#include "me/printf/printf.h"
|
||||
#include "me/string/str_clone.h"
|
||||
#include "me/string/str_len.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#define INT_INLINE_BUF 21
|
||||
|
||||
static void itoa_inner(t_u64 nb, t_str out)
|
||||
{
|
||||
t_u64 modulus;
|
||||
bool need_print;
|
||||
char c;
|
||||
t_usize idx;
|
||||
|
||||
need_print = false;
|
||||
modulus = 1000000000000000000;
|
||||
idx = 0;
|
||||
while (modulus)
|
||||
{
|
||||
c = (char)(nb / modulus) + '0';
|
||||
if (c != '0' || need_print)
|
||||
{
|
||||
out[idx++] = c;
|
||||
need_print = true;
|
||||
}
|
||||
nb = nb % modulus;
|
||||
modulus /= 10;
|
||||
}
|
||||
}
|
||||
|
||||
static t_str itoa_64(t_i64 nb)
|
||||
{
|
||||
char out[INT_INLINE_BUF];
|
||||
t_u64 n;
|
||||
|
||||
n = (t_u64)nb;
|
||||
mem_set(out, 0, INT_INLINE_BUF);
|
||||
if (nb < 0)
|
||||
itoa_inner(-n, out);
|
||||
else if (nb == 0)
|
||||
out[0] = '0';
|
||||
else
|
||||
itoa_inner(n, out);
|
||||
return (str_clone(out));
|
||||
}
|
||||
|
||||
void printf_d(t_printf_arg data, t_printf_func f)
|
||||
{
|
||||
t_u64 value;
|
||||
t_str start_num;
|
||||
t_str sign;
|
||||
t_str void_write;
|
||||
|
||||
value = *(t_i64 *)data.argument;
|
||||
if ((t_u64)value & ((t_u64)1 << 31))
|
||||
sign = "-";
|
||||
else
|
||||
sign = "+";
|
||||
if (!(data.flags & SIGN) && !((t_u64)value & ((t_u64)1 << 31)))
|
||||
sign = "";
|
||||
if (!(data.flags & SIGN) && data.extra.space_align
|
||||
&& !((t_u64)value & ((t_u64)1 << 31)))
|
||||
sign = " ";
|
||||
data.flags |= SIGN;
|
||||
start_num = itoa_64(value);
|
||||
handle_weird_precision_stuff(&data, (t_prec_strs){.out = &start_num,
|
||||
.free_out = true, .pretty = &void_write}, value);
|
||||
pad_and_stuff(
|
||||
(t_pad_and_stuff_args){\
|
||||
.fill_zero = 0, .fill = 0, .sign = sign, .pretty = NULL, .len = \
|
||||
str_len(start_num), .pretty_len = 0, .str = start_num, .allow_zero_fill \
|
||||
= true, .sign_len = str_len(sign), }, data, f);
|
||||
free(start_num);
|
||||
}
|
||||
67
stdme/src/printf/formatter/hex.c
Normal file
67
stdme/src/printf/formatter/hex.c
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* hex.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/16 18:16:16 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/11 19:19:03 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/mem/mem_set.h"
|
||||
#include "me/printf/formatter/utils.h"
|
||||
#include "me/printf/printf.h"
|
||||
#include "me/string/str_len.h"
|
||||
#define HEX_INLINE_BUF 17
|
||||
|
||||
static void fill_hex(t_str out_buf, t_u64 num, t_const_str base)
|
||||
{
|
||||
t_usize i;
|
||||
|
||||
mem_set(out_buf, 0, HEX_INLINE_BUF);
|
||||
out_buf[HEX_INLINE_BUF - 1] = 0;
|
||||
i = 0;
|
||||
while (i < HEX_INLINE_BUF - 1)
|
||||
{
|
||||
out_buf[HEX_INLINE_BUF - i - 2] = base[(num >> (4 * i) & 15)];
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
static void printf_x_common(t_printf_arg data, t_printf_func f,
|
||||
t_const_str pretty, t_const_str base)
|
||||
{
|
||||
t_u64 value;
|
||||
char inline_buffer[HEX_INLINE_BUF];
|
||||
t_str start_num;
|
||||
|
||||
value = *(t_u64 *)data.argument;
|
||||
inline_buffer[0] = '0';
|
||||
inline_buffer[1] = '\0';
|
||||
if (value)
|
||||
fill_hex(inline_buffer, value, base);
|
||||
start_num = &inline_buffer[0];
|
||||
while (start_num[1] != '\0' && start_num[0] == '0')
|
||||
start_num++;
|
||||
if (value == 0)
|
||||
data.extra.pretty = false;
|
||||
handle_weird_precision_stuff(&data, (t_prec_strs){.out = &start_num,
|
||||
.free_out = false, .pretty = (t_str *)&pretty}, value);
|
||||
pad_and_stuff(
|
||||
(t_pad_and_stuff_args){\
|
||||
.fill_zero = 0, .fill = 0, .len = str_len(start_num), .pretty = (t_str)pretty, \
|
||||
.pretty_len = 2, .str = start_num, .allow_zero_fill = true, .sign = NULL, \
|
||||
.sign_len = 0, }, data, f);
|
||||
}
|
||||
|
||||
void printf_x_up(t_printf_arg data, t_printf_func f)
|
||||
{
|
||||
printf_x_common(data, f, "0X", "0123456789ABCDEF");
|
||||
}
|
||||
|
||||
void printf_x_low(t_printf_arg data, t_printf_func f)
|
||||
{
|
||||
printf_x_common(data, f, "0x", "0123456789abcdef");
|
||||
}
|
||||
60
stdme/src/printf/formatter/oct.c
Normal file
60
stdme/src/printf/formatter/oct.c
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* oct.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maix <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/18 01:19:18 by maix #+# #+# */
|
||||
/* Updated: 2023/12/11 19:17:23 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/mem/mem_set.h"
|
||||
#include "me/printf/formatter/utils.h"
|
||||
#include "me/printf/printf.h"
|
||||
#include "me/string/str_len.h"
|
||||
#include <stdio.h>
|
||||
#define OCT_INLINE_BUF 23
|
||||
|
||||
static void fill_oct(t_str out_buf, t_u64 num, t_str base)
|
||||
{
|
||||
t_usize i;
|
||||
|
||||
mem_set(out_buf, 0, OCT_INLINE_BUF);
|
||||
out_buf[OCT_INLINE_BUF - 1] = 0;
|
||||
i = 0;
|
||||
while (i < OCT_INLINE_BUF - 1)
|
||||
{
|
||||
out_buf[OCT_INLINE_BUF - i - 2] = base[(num >> (3 * i) & 7)];
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
void printf_o(t_printf_arg data, t_printf_func f)
|
||||
{
|
||||
t_u64 value;
|
||||
char inline_buffer[OCT_INLINE_BUF];
|
||||
t_str start_num;
|
||||
t_str pretty;
|
||||
|
||||
value = *(t_u64 *)data.argument;
|
||||
inline_buffer[0] = '0';
|
||||
inline_buffer[1] = '\0';
|
||||
pretty = "0o";
|
||||
if (value)
|
||||
fill_oct(inline_buffer, value, "01234567");
|
||||
start_num = &inline_buffer[0];
|
||||
while (start_num[1] != '\0' && start_num[0] == '0')
|
||||
start_num++;
|
||||
if (!value && data.extra.precision == 0 && (data.flags & PRECISION))
|
||||
{
|
||||
start_num = "";
|
||||
pretty = "";
|
||||
}
|
||||
pad_and_stuff(
|
||||
(t_pad_and_stuff_args){\
|
||||
.fill_zero = 0, .fill = 0, .len = str_len(start_num), .pretty = pretty, \
|
||||
.pretty_len = str_len(pretty), .str = start_num, .allow_zero_fill = true, \
|
||||
.sign = NULL, .sign_len = 0, }, data, f);
|
||||
}
|
||||
74
stdme/src/printf/formatter/ptr.c
Normal file
74
stdme/src/printf/formatter/ptr.c
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ptr.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/16 18:16:16 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/11 19:20:42 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/mem/mem_set.h"
|
||||
#include "me/printf/formatter/utils.h"
|
||||
#include "me/printf/printf.h"
|
||||
#include "me/string/str_len.h"
|
||||
#define PTR_INLINE_BUF 17
|
||||
|
||||
static void fill_hex(t_str out_buf, t_u64 num, t_str base)
|
||||
{
|
||||
t_usize i;
|
||||
|
||||
mem_set(out_buf, 0, PTR_INLINE_BUF);
|
||||
out_buf[PTR_INLINE_BUF - 1] = 0;
|
||||
i = 0;
|
||||
while (i < PTR_INLINE_BUF - 1)
|
||||
{
|
||||
out_buf[PTR_INLINE_BUF - i - 2] = base[(num >> (4 * i) & 15)];
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
static void set_values_for_nil(t_usize value, t_str *out, t_printf_arg *data,
|
||||
t_printf_func f)
|
||||
{
|
||||
(void)(f);
|
||||
if (value)
|
||||
{
|
||||
data->flags &= ~PRECISION;
|
||||
data->extra.precision = 0;
|
||||
data->extra.pretty = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
*out = "(nil)";
|
||||
data->extra.precision = 0;
|
||||
data->extra.pretty = false;
|
||||
data->flags &= ~(ZERO_ALIGN | PRECISION);
|
||||
data->flags |= ALIGN;
|
||||
}
|
||||
}
|
||||
|
||||
void printf_p(t_printf_arg data, t_printf_func f)
|
||||
{
|
||||
t_u64 value;
|
||||
char inline_buffer[PTR_INLINE_BUF + 1];
|
||||
t_str start_num;
|
||||
|
||||
value = (t_u64)data.argument;
|
||||
inline_buffer[0] = '0';
|
||||
inline_buffer[1] = '\0';
|
||||
if (value)
|
||||
fill_hex(inline_buffer, value, "0123456789abcdef");
|
||||
start_num = &inline_buffer[0];
|
||||
inline_buffer[PTR_INLINE_BUF] = 0;
|
||||
while (start_num[1] != '\0' && start_num[0] == '0')
|
||||
start_num++;
|
||||
set_values_for_nil(value, &start_num, &data, f);
|
||||
pad_and_stuff(
|
||||
(t_pad_and_stuff_args){\
|
||||
.fill_zero = 0, .fill = 0, .len = str_len(start_num), .pretty = "0x", \
|
||||
.pretty_len = 2, .str = start_num, .allow_zero_fill = value != 0, \
|
||||
.sign = NULL, .sign_len = 0, }, data, f);
|
||||
}
|
||||
76
stdme/src/printf/formatter/unsigned_decimal.c
Normal file
76
stdme/src/printf/formatter/unsigned_decimal.c
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* unsigned_decimal.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maix <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/18 01:44:35 by maix #+# #+# */
|
||||
/* Updated: 2023/12/11 19:19:59 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/mem/mem_alloc_array.h"
|
||||
#include "me/mem/mem_set.h"
|
||||
#include "me/printf/formatter/utils.h"
|
||||
#include "me/printf/printf.h"
|
||||
#include "me/string/str_clone.h"
|
||||
#include "me/string/str_len.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#define UINT_INLINE_BUF 21
|
||||
|
||||
static void itoa_inner(t_u64 nb, t_str out)
|
||||
{
|
||||
t_u64 modulus;
|
||||
bool need_print;
|
||||
char c;
|
||||
t_usize idx;
|
||||
|
||||
need_print = false;
|
||||
modulus = 10000000000000000000u;
|
||||
idx = 0;
|
||||
while (modulus)
|
||||
{
|
||||
c = (char)(nb / modulus) + '0';
|
||||
if (c != '0' || need_print)
|
||||
{
|
||||
out[idx++] = c;
|
||||
need_print = true;
|
||||
}
|
||||
nb = nb % modulus;
|
||||
modulus /= 10;
|
||||
}
|
||||
}
|
||||
|
||||
static t_str itoa_u64(t_u64 nb)
|
||||
{
|
||||
char out[UINT_INLINE_BUF];
|
||||
t_u64 n;
|
||||
|
||||
n = (t_u64)nb;
|
||||
mem_set(out, 0, UINT_INLINE_BUF);
|
||||
if (nb == 0)
|
||||
out[0] = '0';
|
||||
else
|
||||
itoa_inner(n, out);
|
||||
return (str_clone(out));
|
||||
}
|
||||
|
||||
void printf_u(t_printf_arg data, t_printf_func f)
|
||||
{
|
||||
t_u64 value;
|
||||
t_str start_num;
|
||||
t_str void_write;
|
||||
|
||||
value = *(t_u64 *)data.argument;
|
||||
start_num = itoa_u64(value);
|
||||
handle_weird_precision_stuff(&data, (t_prec_strs){.out = &start_num,
|
||||
.free_out = true, .pretty = &void_write}, value);
|
||||
pad_and_stuff(
|
||||
(t_pad_and_stuff_args){\
|
||||
.fill_zero = 0, .fill = 0, .len = str_len(start_num), \
|
||||
.pretty = NULL, .pretty_len = 0, .str = start_num, \
|
||||
.allow_zero_fill = true, .sign = NULL, .sign_len = 0, }, data, f);
|
||||
free(start_num);
|
||||
}
|
||||
136
stdme/src/printf/formatter/utils.c
Normal file
136
stdme/src/printf/formatter/utils.c
Normal file
|
|
@ -0,0 +1,136 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* utils.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/16 17:57:04 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/01 21:20:07 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/buffered_str/buf_str.h"
|
||||
#include "me/convert/atoi.h"
|
||||
#include "me/printf/formatter/utils.h"
|
||||
#include "me/printf/matchers/matchers.h"
|
||||
#include "me/printf/printf.h"
|
||||
#include "me/string/str_find_chr.h"
|
||||
#include "me/string/str_substring.h"
|
||||
#include "me/types.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
bool handle_atoi_stuff(t_const_str fmt, t_usize *c_idx, t_usize *nxt,
|
||||
t_printf_arg *c_arg)
|
||||
{
|
||||
t_i32 atoi_res;
|
||||
|
||||
atoi_res = me_atoi(&fmt[*c_idx]);
|
||||
if (atoi_res < 0)
|
||||
{
|
||||
*c_idx = *nxt;
|
||||
*nxt = (t_usize)(str_find_chr(fmt + *nxt + 1, '%') - fmt);
|
||||
return (false);
|
||||
}
|
||||
advance_atoi(fmt, c_idx);
|
||||
c_arg->extra.align = (t_u64)atoi_res;
|
||||
handle_prec_and_align(fmt, c_idx, c_arg);
|
||||
atoi_res = atoi(&fmt[*c_idx]);
|
||||
if (atoi_res < 0)
|
||||
{
|
||||
*c_idx = *nxt;
|
||||
*nxt = (t_usize)(str_find_chr(fmt + *nxt + 1, '%') - fmt);
|
||||
return (false);
|
||||
}
|
||||
advance_atoi(fmt, c_idx);
|
||||
c_arg->extra.precision = (t_u64)atoi_res;
|
||||
return (true);
|
||||
}
|
||||
|
||||
static void set_flags_if_needed(t_const_str fmt, t_usize *c_idx,
|
||||
t_printf_arg *c_arg)
|
||||
{
|
||||
if (fmt[*c_idx] == ' ')
|
||||
{
|
||||
(*c_idx)++;
|
||||
c_arg->extra.space_align = true;
|
||||
}
|
||||
if (fmt[*c_idx] == '#')
|
||||
{
|
||||
(*c_idx)++;
|
||||
c_arg->extra.pretty = true;
|
||||
}
|
||||
if (fmt[*c_idx] == '+')
|
||||
{
|
||||
(*c_idx)++;
|
||||
c_arg->flags |= SIGN;
|
||||
}
|
||||
if (fmt[*c_idx] == '-')
|
||||
{
|
||||
(*c_idx)++;
|
||||
c_arg->extra.left_align = true;
|
||||
}
|
||||
if (fmt[*c_idx] == '0')
|
||||
{
|
||||
(*c_idx)++;
|
||||
c_arg->flags |= ZERO_ALIGN;
|
||||
}
|
||||
}
|
||||
|
||||
bool set_params(t_const_str fmt, t_usize *c_idx, t_usize *nxt,
|
||||
t_printf_arg *c_arg)
|
||||
{
|
||||
t_usize b_idx;
|
||||
|
||||
b_idx = ~0;
|
||||
while (b_idx != *c_idx)
|
||||
{
|
||||
b_idx = *c_idx;
|
||||
set_flags_if_needed(fmt, c_idx, c_arg);
|
||||
}
|
||||
return (handle_atoi_stuff(fmt, c_idx, nxt, c_arg));
|
||||
}
|
||||
|
||||
t_printf_arg print_substr(t_usize *c_idx, t_usize *nxt, t_const_str fmt,
|
||||
t_pad_inner_args extra)
|
||||
{
|
||||
t_str truc;
|
||||
|
||||
truc = str_substring(fmt, *c_idx, *nxt - *c_idx);
|
||||
extra.f(truc, *nxt - *c_idx, extra.p_args);
|
||||
free(truc);
|
||||
*c_idx = *nxt + 1;
|
||||
return ((t_printf_arg){
|
||||
.p_args = extra.p_args,
|
||||
.argument = extra.arguments,
|
||||
});
|
||||
}
|
||||
|
||||
void pad_inner(t_const_str fmt, t_usize *c_idx, t_usize *nxt,
|
||||
t_pad_inner_args extra)
|
||||
{
|
||||
t_printf_arg c_arg;
|
||||
t_matcher *matcher;
|
||||
|
||||
c_arg = print_substr(c_idx, nxt, fmt, extra);
|
||||
if (fmt[*c_idx] == '%')
|
||||
{
|
||||
(*c_idx)++;
|
||||
extra.f("%", 1, extra.p_args);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!set_params(fmt, c_idx, nxt, &c_arg))
|
||||
return (ret_reset(c_idx, nxt, fmt));
|
||||
matcher = find_matcher(fmt, extra.matchers, c_idx);
|
||||
if (matcher == NULL)
|
||||
return (ret_reset(c_idx, nxt, fmt));
|
||||
call_matcher(matcher, c_arg, *extra.arguments, extra.f);
|
||||
}
|
||||
*nxt = (t_usize)(str_find_chr(fmt + *c_idx, '%'));
|
||||
if (*nxt == 0)
|
||||
return (*nxt = extra.fmt_len, (void)extra.f(fmt + *c_idx, extra.fmt_len
|
||||
- *c_idx, extra.p_args));
|
||||
*nxt = *nxt - (t_usize)fmt;
|
||||
}
|
||||
96
stdme/src/printf/formatter/utils2.c
Normal file
96
stdme/src/printf/formatter/utils2.c
Normal file
|
|
@ -0,0 +1,96 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* utils2.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/16 18:00:07 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/01 21:48:22 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/buffered_str/buf_str.h"
|
||||
#include "me/char/isdigit.h"
|
||||
#include "me/printf/formatter/utils.h"
|
||||
#include "me/printf/printf.h"
|
||||
#include "me/types.h"
|
||||
|
||||
void set_var_for_pad_and_stuff(t_pad_and_stuff_args *a, t_printf_arg *d)
|
||||
{
|
||||
t_isize fill_zero;
|
||||
t_isize fill;
|
||||
|
||||
fill_zero = 0;
|
||||
fill = 0;
|
||||
if (d->flags & ZERO_ALIGN && a->len < d->extra.align)
|
||||
fill_zero = d->extra.align - a->len - a->fill_zero - 2 * d->extra.pretty
|
||||
- a->sign_len;
|
||||
if (d->flags & PRECISION && a->len < d->extra.precision)
|
||||
fill_zero = d->extra.precision - a->len;
|
||||
if (d->flags & (ALIGN) && a->len < d->extra.align)
|
||||
fill = d->extra.align - a->len - a->fill_zero - 2 * d->extra.pretty
|
||||
- a->sign_len - fill_zero;
|
||||
if (fill_zero < 0)
|
||||
fill_zero = 0;
|
||||
if (fill < 0)
|
||||
fill = 0;
|
||||
if (fill_zero < 0)
|
||||
fill_zero = 0;
|
||||
if (fill < 0)
|
||||
fill = 0;
|
||||
a->fill_zero = fill_zero;
|
||||
a->fill = fill;
|
||||
}
|
||||
|
||||
void print_with_func(t_pad_and_stuff_args *a, t_printf_arg *d,
|
||||
t_printf_func f, t_const_str t)
|
||||
{
|
||||
f(t, 1, d->p_args);
|
||||
if (t[0] == ' ' && t[1] == 0 && a->fill)
|
||||
a->fill--;
|
||||
if (t[0] == '0' && t[1] == 0 && a->fill_zero)
|
||||
a->fill_zero--;
|
||||
}
|
||||
|
||||
void pad_and_stuff(t_pad_and_stuff_args a, t_printf_arg d, t_printf_func f)
|
||||
{
|
||||
set_var_for_pad_and_stuff(&a, &d);
|
||||
if (!(d.extra.left_align || d.extra.precision) && d.flags & (ZERO_ALIGN))
|
||||
{
|
||||
print_sign_if_needed(a, d, f);
|
||||
while (a.allow_zero_fill && a.fill_zero > 0)
|
||||
print_with_func(&a, &d, f, "0");
|
||||
}
|
||||
else
|
||||
{
|
||||
while (!(d.extra.left_align) && d.flags & (ALIGN) && a.fill > 0)
|
||||
print_with_func(&a, &d, f, " ");
|
||||
print_sign_if_needed(a, d, f);
|
||||
if (d.extra.pretty)
|
||||
f(a.pretty, a.pretty_len, d.p_args);
|
||||
}
|
||||
while (a.allow_zero_fill && a.fill_zero > 0)
|
||||
print_with_func(&a, &d, f, "0");
|
||||
f(a.str, a.len, d.p_args);
|
||||
while (d.extra.left_align && a.fill > 0)
|
||||
print_with_func(&a, &d, f, " ");
|
||||
}
|
||||
|
||||
void handle_prec_and_align(t_const_str fmt, t_usize *c_idx,
|
||||
t_printf_arg *c_arg)
|
||||
{
|
||||
if (c_arg->extra.align && !(c_arg->flags & ZERO_ALIGN))
|
||||
c_arg->flags |= ALIGN;
|
||||
if (fmt[*c_idx] == '.')
|
||||
{
|
||||
(*c_idx)++;
|
||||
c_arg->flags |= PRECISION;
|
||||
}
|
||||
}
|
||||
|
||||
void advance_atoi(t_const_str fmt, t_usize *idx)
|
||||
{
|
||||
while (me_isdigit(fmt[*idx]))
|
||||
(*idx)++;
|
||||
}
|
||||
53
stdme/src/printf/formatter/utils3.c
Normal file
53
stdme/src/printf/formatter/utils3.c
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* utils3.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/16 18:06:15 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/11 19:21:38 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/printf/formatter/utils.h"
|
||||
#include "me/printf/matchers/matchers.h"
|
||||
#include "me/string/str_find_chr.h"
|
||||
#include "me/string/str_len.h"
|
||||
#include "me/types.h"
|
||||
#include <stdio.h>
|
||||
|
||||
void ret_reset(t_usize *c_idx, t_usize *nxt, t_const_str fmt)
|
||||
{
|
||||
*c_idx = *nxt;
|
||||
*nxt = (t_usize)(str_find_chr(fmt + *nxt + 1, '%') - fmt);
|
||||
}
|
||||
|
||||
void me_printf_str_inner(t_const_str fmt, t_printf_func f,
|
||||
va_list *arguments, void *p_args)
|
||||
{
|
||||
t_usize c_idx;
|
||||
t_usize nxt;
|
||||
t_usize fmt_len;
|
||||
|
||||
c_idx = 0;
|
||||
fmt_len = str_len(fmt);
|
||||
nxt = (t_usize)(str_find_chr(fmt, '%'));
|
||||
if (nxt == 0)
|
||||
return (f(fmt, fmt_len, p_args));
|
||||
nxt = nxt - (t_usize)fmt;
|
||||
while (nxt < fmt_len)
|
||||
{
|
||||
pad_inner(fmt, &c_idx, &nxt, \
|
||||
((t_pad_inner_args){\
|
||||
.p_args = p_args, .fmt_len = fmt_len, .f = f, \
|
||||
.arguments = arguments, .matchers = get_matchers(), }));
|
||||
}
|
||||
}
|
||||
|
||||
void print_sign_if_needed(t_pad_and_stuff_args a, t_printf_arg d,
|
||||
t_printf_func f)
|
||||
{
|
||||
if (d.flags & (SIGN) && a.sign != NULL)
|
||||
f(a.sign, a.sign_len, d.p_args);
|
||||
}
|
||||
36
stdme/src/printf/formatter/utils_numbers.c
Normal file
36
stdme/src/printf/formatter/utils_numbers.c
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* utils_numbers.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/01 21:05:47 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/01 21:49:51 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/mem/mem_alloc_array.h"
|
||||
#include "me/printf/formatter/utils.h"
|
||||
#include "me/printf/printf.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
void handle_weird_precision_stuff(t_printf_arg *data, t_prec_strs strs,
|
||||
t_usize value)
|
||||
{
|
||||
if (!value && data->extra.precision == 0 && (data->flags & PRECISION))
|
||||
{
|
||||
data->flags &= (~ZERO_ALIGN);
|
||||
data->flags |= ALIGN;
|
||||
if (strs.free_out)
|
||||
*strs.out = (free(*strs.out), (t_str)mem_alloc_array(1, 1));
|
||||
else
|
||||
*strs.out = "";
|
||||
*strs.pretty = "";
|
||||
}
|
||||
else if ((data->flags & PRECISION) && !data->extra.left_align)
|
||||
{
|
||||
data->flags &= (~ZERO_ALIGN);
|
||||
data->flags |= ALIGN;
|
||||
}
|
||||
}
|
||||
120
stdme/src/printf/matchers.c
Normal file
120
stdme/src/printf/matchers.c
Normal file
|
|
@ -0,0 +1,120 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* matchers.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/16 18:07:40 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/11 19:11:51 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/mem/mem_compare.h"
|
||||
#include "me/printf/formatter/formatter.h"
|
||||
#include "me/printf/matchers/matchers.h"
|
||||
#include "me/printf/printf.h"
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
t_matcher_list *get_matchers(void)
|
||||
{
|
||||
static t_matcher_list printf_matchers = (t_matcher_list){
|
||||
.data = {
|
||||
{.matcher = "o", .matcher_len = 1, .arg_type = U64, .f = &printf_o},
|
||||
{.matcher = "c", .matcher_len = 1, .arg_type = CHAR, .f = &printf_c},
|
||||
{.matcher = "s", .matcher_len = 1, .arg_type = STR, .f = &printf_s},
|
||||
{.matcher = "p", .matcher_len = 1, .arg_type = VOID_PTR, .f = &printf_p},
|
||||
{.matcher = "d", .matcher_len = 1, .arg_type = I32, .f = &printf_d},
|
||||
{.matcher = "i", .matcher_len = 1, .arg_type = I32, .f = &printf_d},
|
||||
{.matcher = "u", .matcher_len = 1, .arg_type = U32, .f = &printf_u},
|
||||
{.matcher = "x", .matcher_len = 1, .arg_type = U32, .f = &printf_x_low},
|
||||
{.matcher = "X", .matcher_len = 1, .arg_type = U32, .f = &printf_x_up},
|
||||
},
|
||||
.next = NULL,
|
||||
};
|
||||
|
||||
return (&printf_matchers);
|
||||
}
|
||||
|
||||
bool insert_matcher(t_matcher matcher)
|
||||
{
|
||||
t_matcher_list *matchers;
|
||||
t_usize i;
|
||||
|
||||
matchers = get_matchers();
|
||||
while (matchers)
|
||||
{
|
||||
i = 0;
|
||||
while (i < PRINTF_BUFFER_CHUNK)
|
||||
{
|
||||
if (matchers->data[i].f == NULL)
|
||||
{
|
||||
(*matchers).data[i] = matcher;
|
||||
return (true);
|
||||
}
|
||||
}
|
||||
matchers->next = malloc(sizeof(t_matcher_list) * 1);
|
||||
}
|
||||
return (false);
|
||||
}
|
||||
|
||||
t_matcher *find_matcher(t_const_str fmt, t_matcher_list *matchers,
|
||||
t_usize *c_idx)
|
||||
{
|
||||
t_usize matcher_index;
|
||||
t_matcher *matcher;
|
||||
|
||||
while (matchers)
|
||||
{
|
||||
matcher_index = 0;
|
||||
while (matcher_index < PRINTF_BUFFER_CHUNK)
|
||||
{
|
||||
matcher = &matchers->data[matcher_index];
|
||||
if (matcher->f)
|
||||
{
|
||||
if (!mem_compare(&fmt[*c_idx], matcher->matcher,
|
||||
matcher->matcher_len))
|
||||
{
|
||||
*c_idx += matcher->matcher_len;
|
||||
return (matcher);
|
||||
}
|
||||
}
|
||||
matcher_index++;
|
||||
}
|
||||
matchers = matchers->next;
|
||||
}
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
// FIGURE OUT HOW TO MAKE I64 WORK
|
||||
void call_matcher(t_matcher *matcher, t_printf_arg matcher_arguments,
|
||||
va_list args, t_printf_func f)
|
||||
{
|
||||
t_matcher_tmp_val vals;
|
||||
|
||||
matcher_arguments.argument = NULL;
|
||||
if (matcher->arg_type == CHAR)
|
||||
{
|
||||
vals.chr_val = (char)va_arg(args, int);
|
||||
matcher_arguments.argument = (void *)&vals.chr_val;
|
||||
}
|
||||
if (matcher->arg_type == U32)
|
||||
vals.u64_val = va_arg(args, t_u32);
|
||||
if (matcher->arg_type == U64)
|
||||
vals.u64_val = va_arg(args, t_u64);
|
||||
if (matcher->arg_type == I64)
|
||||
vals.i64_val = va_arg(args, t_i64);
|
||||
if (matcher->arg_type == I32)
|
||||
vals.i64_val = va_arg(args, t_i32);
|
||||
if (matcher->arg_type == I32 || matcher->arg_type == I64)
|
||||
matcher_arguments.argument = (void *)&vals.i64_val;
|
||||
if (matcher->arg_type == U32 || matcher->arg_type == U64)
|
||||
matcher_arguments.argument = (void *)&vals.u64_val;
|
||||
if (matcher->arg_type == VOID_PTR)
|
||||
matcher_arguments.argument = (void *)va_arg(args, void *);
|
||||
if (matcher->arg_type == STR)
|
||||
matcher_arguments.argument = (void *)va_arg(args, t_str);
|
||||
matcher->f(matcher_arguments, f);
|
||||
}
|
||||
120
stdme/src/printf/printf.c
Normal file
120
stdme/src/printf/printf.c
Normal file
|
|
@ -0,0 +1,120 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* printf.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/11 17:50:56 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/02/09 14:58:10 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/buffered_str/buf_str.h"
|
||||
#include "me/fs/write.h"
|
||||
#include "me/printf/formatter/formatter.h"
|
||||
#include "me/printf/formatter/utils.h"
|
||||
#include "me/printf/matchers/matchers.h"
|
||||
#include "me/printf/printf.h"
|
||||
#include "me/string/str_len.h"
|
||||
#include "me/types.h"
|
||||
#include <limits.h>
|
||||
#include <stdarg.h>
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
// p_args is an t_buffer_str;
|
||||
static void me_printf_add_to_string(t_const_str to_write, t_usize to_write_len,
|
||||
void *p_args)
|
||||
{
|
||||
t_buffer_str *out_buf;
|
||||
|
||||
out_buf = (t_buffer_str *)p_args;
|
||||
(void)(to_write_len);
|
||||
push_str_buffer(out_buf, to_write);
|
||||
}
|
||||
|
||||
t_str me_printf_str(t_const_str fmt, va_list *arguments)
|
||||
{
|
||||
t_buffer_str out;
|
||||
|
||||
out = alloc_new_buffer(str_len(fmt));
|
||||
if (out.buf == NULL)
|
||||
{
|
||||
return (NULL);
|
||||
}
|
||||
me_printf_str_inner(fmt, &me_printf_add_to_string, arguments, (void *)&out);
|
||||
return (out.buf);
|
||||
}
|
||||
|
||||
void me_printf_write(t_const_str to_write, t_usize to_write_len,
|
||||
void *p_args)
|
||||
{
|
||||
t_fprintf_arg *arg;
|
||||
|
||||
arg = (t_fprintf_arg *)p_args;
|
||||
me_write(arg->fd, (t_u8 *)to_write, to_write_len);
|
||||
arg->total_print += to_write_len;
|
||||
}
|
||||
|
||||
t_usize me_printf(t_const_str fmt, ...)
|
||||
{
|
||||
va_list args;
|
||||
t_fprintf_arg passthru;
|
||||
|
||||
passthru = (t_fprintf_arg){
|
||||
.fd = 1,
|
||||
.total_print = 0,
|
||||
};
|
||||
va_start(args, fmt);
|
||||
me_printf_str_inner(fmt, &me_printf_write, &args, (void *)&passthru);
|
||||
va_end(args);
|
||||
return (passthru.total_print);
|
||||
}
|
||||
|
||||
t_usize me_eprintf(t_const_str fmt, ...)
|
||||
{
|
||||
va_list args;
|
||||
t_fprintf_arg passthru;
|
||||
|
||||
passthru = (t_fprintf_arg){
|
||||
.fd = 2,
|
||||
.total_print = 0,
|
||||
};
|
||||
va_start(args, fmt);
|
||||
me_printf_str_inner(fmt, &me_printf_write, &args, (void *)&passthru);
|
||||
va_end(args);
|
||||
return (passthru.total_print);
|
||||
}
|
||||
|
||||
/*
|
||||
t_usize me_printf(t_const_str fmt, ...)
|
||||
{
|
||||
va_list args;
|
||||
t_str str;
|
||||
t_usize len;
|
||||
|
||||
va_start(args, fmt);
|
||||
str = me_printf_str(fmt, &args);
|
||||
va_end(args);
|
||||
len = str_len(str);
|
||||
write(1, str, len);
|
||||
free(str);
|
||||
return (len);
|
||||
}
|
||||
|
||||
t_usize me_eprintf(t_const_str fmt, ...)
|
||||
{
|
||||
va_list args;
|
||||
t_str str;
|
||||
t_usize len;
|
||||
|
||||
va_start(args, fmt);
|
||||
str = me_printf_str(fmt, &args);
|
||||
va_end(args);
|
||||
len = str_len(str);
|
||||
write(2, str, len);
|
||||
free(str);
|
||||
return (len);
|
||||
}
|
||||
*/
|
||||
51
stdme/src/printf/vprintf.c
Normal file
51
stdme/src/printf/vprintf.c
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* vprintf.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/09 14:57:28 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/02/09 15:00:39 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/buffered_str/buf_str.h"
|
||||
#include "me/fs/write.h"
|
||||
#include "me/printf/formatter/formatter.h"
|
||||
#include "me/printf/formatter/utils.h"
|
||||
#include "me/printf/matchers/matchers.h"
|
||||
#include "me/printf/printf.h"
|
||||
#include "me/string/str_len.h"
|
||||
#include "me/types.h"
|
||||
#include <limits.h>
|
||||
#include <stdarg.h>
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
void me_printf_write(t_const_str to_write, t_usize to_write_len,
|
||||
void *p_args);
|
||||
|
||||
t_usize me_vprintf(t_const_str fmt, va_list *args)
|
||||
{
|
||||
t_fprintf_arg passthru;
|
||||
|
||||
passthru = (t_fprintf_arg){
|
||||
.fd = 1,
|
||||
.total_print = 0,
|
||||
};
|
||||
me_printf_str_inner(fmt, &me_printf_write, args, (void *)&passthru);
|
||||
return (passthru.total_print);
|
||||
}
|
||||
|
||||
t_usize me_veprintf(t_const_str fmt, va_list *args)
|
||||
{
|
||||
t_fprintf_arg passthru;
|
||||
|
||||
passthru = (t_fprintf_arg){
|
||||
.fd = 2,
|
||||
.total_print = 0,
|
||||
};
|
||||
me_printf_str_inner(fmt, &me_printf_write, args, (void *)&passthru);
|
||||
return (passthru.total_print);
|
||||
}
|
||||
30
stdme/src/string/str_clone.c
Normal file
30
stdme/src/string/str_clone.c
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* str_clone.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/06 16:05:48 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/09 18:15:57 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/mem/mem_alloc.h"
|
||||
#include "me/string/str_clone.h"
|
||||
#include "me/string/str_l_copy.h"
|
||||
#include "me/string/str_len.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
t_str str_clone(t_const_str source)
|
||||
{
|
||||
t_str res;
|
||||
t_usize len;
|
||||
|
||||
len = str_len(source) + 1;
|
||||
res = mem_alloc(sizeof(unsigned char) * len);
|
||||
if (res == NULL)
|
||||
return (NULL);
|
||||
str_l_copy(res, source, len);
|
||||
return (res);
|
||||
}
|
||||
29
stdme/src/string/str_find_chr.c
Normal file
29
stdme/src/string/str_find_chr.c
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* str_find_chr.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/04 17:29:13 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/09 14:47:43 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/string/str_find_chr.h"
|
||||
|
||||
char *str_find_chr(t_const_str str, char chr)
|
||||
{
|
||||
t_usize index;
|
||||
|
||||
index = 0;
|
||||
while (str[index])
|
||||
{
|
||||
if (str[index] == chr)
|
||||
return ((char *)&str[index]);
|
||||
index++;
|
||||
}
|
||||
if (str[index] == chr)
|
||||
return ((char *)&str[index]);
|
||||
return (NULL);
|
||||
}
|
||||
30
stdme/src/string/str_find_rev_chr.c
Normal file
30
stdme/src/string/str_find_rev_chr.c
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* str_find_rev_chr.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/04 17:29:13 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/04/28 19:42:46 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/string/str_find_rev_chr.h"
|
||||
#include "me/string/str_len.h"
|
||||
|
||||
char *str_find_rev_chr(t_const_str str, char chr)
|
||||
{
|
||||
t_usize index;
|
||||
|
||||
if (str == NULL)
|
||||
return (NULL);
|
||||
index = str_len((t_str)str);
|
||||
while (index >= 0)
|
||||
{
|
||||
if (str[index] == chr)
|
||||
return ((char *)&str[index]);
|
||||
index--;
|
||||
}
|
||||
return (NULL);
|
||||
}
|
||||
56
stdme/src/string/str_find_str.c
Normal file
56
stdme/src/string/str_find_str.c
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* str_find_str.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/08/10 11:11:01 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/09 16:53:53 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/string/str_n_find_str.h"
|
||||
|
||||
const char *str_find_str(t_const_str str, t_const_str to_find)
|
||||
{
|
||||
t_str needle;
|
||||
t_str haystack;
|
||||
|
||||
if (*to_find == '\0')
|
||||
return (str);
|
||||
while (*str)
|
||||
{
|
||||
haystack = (t_str)str;
|
||||
needle = (t_str)to_find;
|
||||
while (*haystack && *haystack == *needle)
|
||||
{
|
||||
haystack++;
|
||||
needle++;
|
||||
}
|
||||
if (*needle == '\0')
|
||||
return (str);
|
||||
str++;
|
||||
}
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
/*R
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
if (argc != 3)
|
||||
return (2);
|
||||
printf("HAYSTACK = '%s'\n", argv[1]);
|
||||
printf(" NEEDLE = '%s'\n", argv[2]);
|
||||
|
||||
printf("libc: %p : '%s'\n", str_find_str(argv[1], argv[2]),
|
||||
str_find_str(argv[1],
|
||||
argv[2]));
|
||||
printf(" ft : %p : '%s'\n",
|
||||
str_find_str(argv[1], argv[2]),
|
||||
str_find_str(argv[1], argv[2]));
|
||||
}
|
||||
R*/
|
||||
27
stdme/src/string/str_iter.c
Normal file
27
stdme/src/string/str_iter.c
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* str_iter.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/09 18:26:00 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/09 14:49:47 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/string/str_iter.h"
|
||||
|
||||
void str_iter(t_str s, void (*f)(t_usize, char *))
|
||||
{
|
||||
t_usize idx;
|
||||
|
||||
if (s == NULL)
|
||||
return ;
|
||||
idx = 0;
|
||||
while (s[idx])
|
||||
{
|
||||
f(idx, &s[idx]);
|
||||
idx++;
|
||||
}
|
||||
}
|
||||
34
stdme/src/string/str_join.c
Normal file
34
stdme/src/string/str_join.c
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* str_join.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/07 23:02:58 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/09 18:16:19 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/mem/mem_alloc.h"
|
||||
#include "me/string/str_join.h"
|
||||
#include "me/string/str_l_cat.h"
|
||||
#include "me/string/str_l_copy.h"
|
||||
#include "me/string/str_len.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
t_str str_join(t_const_str s1, t_const_str s2)
|
||||
{
|
||||
t_str out;
|
||||
t_usize buf_size;
|
||||
|
||||
if (s1 == NULL || s2 == NULL)
|
||||
return (NULL);
|
||||
buf_size = str_len(s1) + str_len(s2) + 1;
|
||||
out = mem_alloc(sizeof(char) * buf_size);
|
||||
if (out == NULL)
|
||||
return (NULL);
|
||||
str_l_copy(out, s1, buf_size);
|
||||
str_l_cat(out, s2, buf_size);
|
||||
return (out);
|
||||
}
|
||||
108
stdme/src/string/str_l_cat.c
Normal file
108
stdme/src/string/str_l_cat.c
Normal file
|
|
@ -0,0 +1,108 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* str_l_cat.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/08/09 18:01:09 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/09 14:51:34 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
/*R
|
||||
|
||||
//CFLAGS="-lbsd"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
R*/
|
||||
|
||||
#include "me/string/str_l_cat.h"
|
||||
#include "me/string/str_len.h"
|
||||
|
||||
t_usize str_l_cat(t_str dest, t_const_str src, t_usize buffer_size)
|
||||
{
|
||||
t_usize dest_len;
|
||||
t_usize src_len;
|
||||
t_usize i;
|
||||
|
||||
if (buffer_size == 0 && (dest == NULL || src == NULL))
|
||||
return (0);
|
||||
dest_len = str_len(dest);
|
||||
src_len = str_len(src);
|
||||
if (dest_len >= buffer_size)
|
||||
return (buffer_size + src_len);
|
||||
i = 0;
|
||||
while (src[i] && dest_len + i < buffer_size - 1)
|
||||
{
|
||||
dest[dest_len + i] = src[i];
|
||||
i++;
|
||||
}
|
||||
dest[dest_len + i] = '\0';
|
||||
return (dest_len + src_len);
|
||||
}
|
||||
|
||||
/*R
|
||||
|
||||
#include <bsd/string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define SIZE 64
|
||||
|
||||
int main(void) {
|
||||
char* dest_ft;
|
||||
char* dest_libc;
|
||||
char* to_cat = "banane";
|
||||
unsigned int res_libc = 0;
|
||||
unsigned int res_ft = 0;
|
||||
int i;
|
||||
|
||||
for (int v = 0; v <= SIZE; v++)
|
||||
for (int j = 0; j < SIZE; j++)
|
||||
{
|
||||
dest_ft = malloc(SIZE);
|
||||
dest_libc = malloc(SIZE);
|
||||
i = 0;
|
||||
while (i < SIZE)
|
||||
{
|
||||
dest_ft[i] = 'X';
|
||||
dest_libc[i] = 'X';
|
||||
i++;
|
||||
}
|
||||
dest_ft[SIZE - j - 1] = 0;
|
||||
dest_libc[SIZE - j - 1] = 0;
|
||||
res_libc = str_l_cat(dest_libc, to_cat, SIZE - v);
|
||||
res_ft = str_l_cat(dest_ft, to_cat, SIZE - v);
|
||||
int k;
|
||||
k = 0;
|
||||
while (k < SIZE && dest_libc[k] == dest_ft[k])
|
||||
k++;
|
||||
|
||||
if (strcmp(dest_ft, dest_libc) != 0 || res_ft != res_libc
|
||||
|| k != SIZE)
|
||||
{
|
||||
printf("----------v[%d]-j[%d]---------\n", v, j);
|
||||
printf("libc: [%d]\t'%s'\n", res_libc ,dest_libc);
|
||||
printf(" ft : [%d]\t'%s'\n", res_ft, dest_ft);
|
||||
printf("[ERROR] byte %d is different: LIBC[%X] != FT[%X]\n\n",
|
||||
i,
|
||||
dest_libc[j], dest_ft[j]);
|
||||
printf(" ft :");
|
||||
for (int x = 0; x < SIZE; x++)
|
||||
printf("%02X", dest_ft[x]);
|
||||
printf("\n");
|
||||
printf("libc:");
|
||||
for (int x = 0; x < SIZE; x++)
|
||||
printf("%02X", dest_libc[x]);
|
||||
printf("\n");
|
||||
printf("\n");
|
||||
}
|
||||
free(dest_libc);
|
||||
free(dest_ft);
|
||||
}
|
||||
}
|
||||
R*/
|
||||
104
stdme/src/string/str_l_copy.c
Normal file
104
stdme/src/string/str_l_copy.c
Normal file
|
|
@ -0,0 +1,104 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* str_l_copy.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/08/09 18:01:09 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/09 14:51:55 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
/*R
|
||||
|
||||
//CFLAGS="-lbsd"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
R*/
|
||||
|
||||
#include "me/string/str_l_copy.h"
|
||||
#include "me/string/str_len.h"
|
||||
|
||||
t_usize str_l_copy(t_str dest, t_const_str src, t_usize buffer_size)
|
||||
{
|
||||
t_usize src_len;
|
||||
t_usize i;
|
||||
|
||||
src_len = str_len(src);
|
||||
i = 0;
|
||||
if (buffer_size == 0)
|
||||
return (src_len);
|
||||
while (src[i] && i < buffer_size - 1)
|
||||
{
|
||||
dest[i] = src[i];
|
||||
i++;
|
||||
}
|
||||
dest[i] = '\0';
|
||||
return (src_len);
|
||||
}
|
||||
|
||||
/*R
|
||||
|
||||
#include <bsd/string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define SIZE 64
|
||||
|
||||
int main(void) {
|
||||
char* dest_ft;
|
||||
char* dest_libc;
|
||||
char* to_cat = "banane";
|
||||
unsigned int res_libc = 0;
|
||||
unsigned int res_ft = 0;
|
||||
int i;
|
||||
|
||||
for (int v = 0; v <= SIZE; v++)
|
||||
for (int j = 0; j < SIZE; j++)
|
||||
{
|
||||
dest_ft = malloc(SIZE);
|
||||
dest_libc = malloc(SIZE);
|
||||
i = 0;
|
||||
while (i < SIZE)
|
||||
{
|
||||
dest_ft[i] = 'X';
|
||||
dest_libc[i] = 'X';
|
||||
i++;
|
||||
}
|
||||
dest_ft[SIZE - j - 1] = 0;
|
||||
dest_libc[SIZE - j - 1] = 0;
|
||||
res_libc = str_l_cat(dest_libc, to_cat, SIZE - v);
|
||||
res_ft = str_l_cat(dest_ft, to_cat, SIZE - v);
|
||||
int k;
|
||||
k = 0;
|
||||
while (k < SIZE && dest_libc[k] == dest_ft[k])
|
||||
k++;
|
||||
|
||||
if (strcmp(dest_ft, dest_libc) != 0 || res_ft != res_libc
|
||||
|| k != SIZE)
|
||||
{
|
||||
printf("----------v[%d]-j[%d]---------\n", v, j);
|
||||
printf("libc: [%d]\t'%s'\n", res_libc ,dest_libc);
|
||||
printf(" ft : [%d]\t'%s'\n", res_ft, dest_ft);
|
||||
printf("[ERROR] byte %d is different: LIBC[%X] != FT[%X]\n\n",
|
||||
i,
|
||||
dest_libc[j], dest_ft[j]);
|
||||
printf(" ft :");
|
||||
for (int x = 0; x < SIZE; x++)
|
||||
printf("%02X", dest_ft[x]);
|
||||
printf("\n");
|
||||
printf("libc:");
|
||||
for (int x = 0; x < SIZE; x++)
|
||||
printf("%02X", dest_libc[x]);
|
||||
printf("\n");
|
||||
printf("\n");
|
||||
}
|
||||
free(dest_libc);
|
||||
free(dest_ft);
|
||||
}
|
||||
}
|
||||
R*/
|
||||
25
stdme/src/string/str_len.c
Normal file
25
stdme/src/string/str_len.c
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* str_len.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/04 17:07:41 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/03/08 12:49:41 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/string/str_len.h"
|
||||
|
||||
t_usize str_len(t_const_str str)
|
||||
{
|
||||
t_usize out;
|
||||
|
||||
if (str == NULL)
|
||||
return (0);
|
||||
out = 0;
|
||||
while (str[out])
|
||||
out++;
|
||||
return (out);
|
||||
}
|
||||
33
stdme/src/string/str_map.c
Normal file
33
stdme/src/string/str_map.c
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* str_map.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/09 18:26:00 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/09 14:52:34 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/string/str_clone.h"
|
||||
#include "me/string/str_map.h"
|
||||
|
||||
t_str str_map(t_const_str s, char (*f)(t_usize, char))
|
||||
{
|
||||
t_str out;
|
||||
t_usize idx;
|
||||
|
||||
if (f == NULL || s == NULL)
|
||||
return (NULL);
|
||||
out = str_clone((t_str)s);
|
||||
if (out == NULL)
|
||||
return (NULL);
|
||||
idx = 0;
|
||||
while (s[idx])
|
||||
{
|
||||
out[idx] = f(idx, s[idx]);
|
||||
idx++;
|
||||
}
|
||||
return (out);
|
||||
}
|
||||
27
stdme/src/string/str_n_compare.c
Normal file
27
stdme/src/string/str_n_compare.c
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* str_n_compare.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/04 18:53:47 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/09 14:52:48 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/string/str_n_compare.h"
|
||||
|
||||
// PLEASE FIX THIS FUNCTION IF NEEDED !
|
||||
t_i32 str_n_compare(t_const_str lhs, t_const_str rhs, t_usize n)
|
||||
{
|
||||
t_usize index;
|
||||
|
||||
index = 0;
|
||||
if (n == 0)
|
||||
return (0);
|
||||
while (lhs[index] && rhs[index] && lhs[index] == rhs[index] && index < n
|
||||
- 1)
|
||||
index++;
|
||||
return ((t_i32)(t_u8)lhs[index] - (t_i32)(t_u8)rhs[index]);
|
||||
}
|
||||
51
stdme/src/string/str_n_find_str.c
Normal file
51
stdme/src/string/str_n_find_str.c
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* str_n_find_str.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/08/10 11:11:01 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/04/28 19:40:32 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/string/str_len.h"
|
||||
#include "me/string/str_n_find_str.h"
|
||||
|
||||
static t_str local_get_end_of_search(t_usize len, t_str str)
|
||||
{
|
||||
t_usize out_len;
|
||||
|
||||
out_len = str_len(str);
|
||||
if (len > out_len)
|
||||
len = out_len;
|
||||
return (str + len);
|
||||
}
|
||||
|
||||
const char *str_n_find_str(t_const_str str, t_const_str to_find, t_usize len)
|
||||
{
|
||||
t_str needle;
|
||||
t_str haystack;
|
||||
t_str end_of_search;
|
||||
|
||||
if ((str == NULL || to_find == NULL))
|
||||
return (NULL);
|
||||
if (*to_find == '\0')
|
||||
return ((t_str)str);
|
||||
end_of_search = local_get_end_of_search(len, (t_str)str);
|
||||
while (*str)
|
||||
{
|
||||
haystack = (t_str)str;
|
||||
needle = (t_str)to_find;
|
||||
while (haystack < end_of_search && *haystack && *haystack == *needle)
|
||||
{
|
||||
haystack++;
|
||||
needle++;
|
||||
}
|
||||
if (*needle == '\0')
|
||||
return (str);
|
||||
str++;
|
||||
}
|
||||
return (NULL);
|
||||
}
|
||||
93
stdme/src/string/str_split.c
Normal file
93
stdme/src/string/str_split.c
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* str_split.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/08/17 15:56:59 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/09 16:52:08 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/mem/mem_alloc.h"
|
||||
#include "me/mem/mem_alloc_array.h"
|
||||
#include "me/string/str_l_copy.h"
|
||||
#include "me/string/str_split.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
static t_usize local_count_words(t_const_str str, char chr);
|
||||
static t_str *local_split_inner(t_const_str str, char chr, t_str *out);
|
||||
static t_str *local_split_freeall(t_str **to_free);
|
||||
|
||||
static t_usize local_count_words(t_const_str str, char chr)
|
||||
{
|
||||
t_usize i;
|
||||
t_usize out;
|
||||
|
||||
out = 0;
|
||||
i = 0;
|
||||
while (str[i])
|
||||
{
|
||||
while (str[i] && str[i] == chr)
|
||||
i++;
|
||||
if (str[i] == 0)
|
||||
return (out);
|
||||
out++;
|
||||
while (str[i] && str[i] != chr)
|
||||
i++;
|
||||
}
|
||||
return (out);
|
||||
}
|
||||
|
||||
static t_str *local_split_freeall(t_str **to_free)
|
||||
{
|
||||
while (*to_free)
|
||||
free(*(to_free++));
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
static t_str *local_split_inner(t_const_str str, char chr, t_str *out)
|
||||
{
|
||||
t_usize str_i;
|
||||
t_usize sub_i;
|
||||
t_usize ptr_i;
|
||||
|
||||
str_i = 0;
|
||||
ptr_i = 0;
|
||||
while (str[str_i])
|
||||
{
|
||||
while (str[str_i] && str[str_i] == chr)
|
||||
str_i++;
|
||||
if (str[str_i] == 0)
|
||||
break ;
|
||||
sub_i = 0;
|
||||
while (str[str_i + sub_i] && str[str_i + sub_i] != chr)
|
||||
sub_i++;
|
||||
out[ptr_i] = mem_alloc(sizeof(char) * (sub_i + 1));
|
||||
if (out[ptr_i] == NULL)
|
||||
return (local_split_freeall(&out));
|
||||
str_l_copy(out[ptr_i++], (t_str)(str + str_i), sub_i + 1);
|
||||
str_i += sub_i;
|
||||
}
|
||||
out[ptr_i] = NULL;
|
||||
return (out);
|
||||
}
|
||||
|
||||
t_str *str_split(t_const_str str, char chr)
|
||||
{
|
||||
t_usize ptr_len;
|
||||
t_str *out;
|
||||
|
||||
if (str == NULL || *str == 0)
|
||||
{
|
||||
out = mem_alloc(sizeof(t_str) * 1);
|
||||
*out = NULL;
|
||||
return (out);
|
||||
}
|
||||
ptr_len = local_count_words(str, chr);
|
||||
out = mem_alloc_array(sizeof(t_str), (ptr_len + 1));
|
||||
if (out == NULL)
|
||||
return (NULL);
|
||||
return (local_split_inner(str, chr, out));
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue