Permalink
Browse files

added shitty parser

  • Loading branch information...
Tac
Tac committed Nov 14, 2017
1 parent 2d96000 commit 15613d47a694c47f5158ae72de4d1e8b1b0bd383
Showing with 228 additions and 71 deletions.
  1. +1 −1 Makefile
  2. +9 −2 default.xml
  3. +7 −5 includes/rtv1.h
  4. +2 −2 libft/ft_memdel.c
  5. +2 −3 libft/ft_strstr.c
  6. +2 −2 libft/ft_strtrim_free.c
  7. +6 −5 src/init.c
  8. +2 −2 src/main.c
  9. +2 −2 src/tools.c
  10. +107 −47 src/xml_file.c
  11. +88 −0 src/xml_file.c.bak
View
@@ -84,7 +84,7 @@ gpu_flags:
$(eval GPU_L = -framework OpenCL)
$(eval GPU_MACRO = -DGPU)
debug: clean cleanlibft debuglibft debug_flag
debug: clean cleanlibft debuglibft debug_flag gpu
debug_flag:
$(eval DEBUG_MACRO = -DDEBUG -g)
$(eval ASANFLAGS = -fsanitize=address -fno-omit-frame-pointer)
View
@@ -1,5 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- cul -->
<!--
Comments cannot appear before XML declaration. -> semble ok car verif header
Comments may appear anywhere in a document. -> semble ok
Comments must not appear within attribute values. -> à faire
Comments cannot be nested inside the other comments. -> semble OK
<cam id="0"
pos="0, 0, 0"
hor="0.6, 0, 0"
@@ -28,7 +35,7 @@
color="0x00ffffff"
/>
<plane id="0"
<plane id="0"
pos="0, 0, 0"
normale="0, 0, 1"
color="0x00ff0000"
@@ -39,8 +46,8 @@
dir="0, 0, 0"
color="0x00ff0000"
/>
-->
<!-- bite -->
<scene>
<cam id="0"
pos="-20, 0, 50"
View
@@ -120,6 +120,9 @@ typedef struct s_param
typedef struct s_xml
{
char *scene;
int scene_fd;
char comment;
unsigned int lbra;
unsigned int rbra;
unsigned int slas;
@@ -155,8 +158,7 @@ typedef struct s_env
int mou_x;
int mou_y;
int debug;
char *scene;
int scene_fd;
t_xml *xml;
char *kernel_src;
cl_device_id device_id;
cl_context context;
@@ -191,7 +193,9 @@ typedef struct s_env
***** comment template *****
*/
void init(t_env *e);
void get_file(t_env *e, int ac, char *av);
void init(t_env *e, int ac, char *av);
void set_hooks(t_env *e);
int quit(t_env *e);
void error(void);
@@ -208,8 +212,6 @@ int mlx_key_release(int key, t_env *e);
int mlx_key_press(int key, t_env *e);
int mlx_key_simple(int key, t_env *e);
void get_file(t_env *e, int ac, char *av);
int opencl_init(t_env *e);
int opencl_allocate_scene_memory(t_env *e);
int draw(t_env *e);
View
@@ -3,10 +3,10 @@
/* ::: :::::::: */
/* ft_memdel.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fmessina <marvin@42.fr> +#+ +:+ +#+ */
/* By: fmessina <fmessina@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/14 19:20:08 by fmessina #+# #+# */
/* Updated: 2016/11/23 16:38:58 by fmessina ### ########.fr */
/* Updated: 2017/11/13 22:04:57 by fmessina ### ########.fr */
/* */
/* ************************************************************************** */
View
@@ -3,15 +3,14 @@
/* ::: :::::::: */
/* ft_strstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fmessina <marvin@42.fr> +#+ +:+ +#+ */
/* By: fmessina <fmessina@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/21 17:56:01 by fmessina #+# #+# */
/* Updated: 2016/11/23 17:24:59 by fmessina ### ########.fr */
/* Updated: 2017/11/12 18:02:43 by fmessina ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <string.h>
char *ft_strstr(char const *s1, char const *s2)
{
View
@@ -3,10 +3,10 @@
/* ::: :::::::: */
/* ft_strtrim_free.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fmessina <marvin@42.fr> +#+ +:+ +#+ */
/* By: fmessina <fmessina@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/09/29 17:53:34 by fmessina #+# #+# */
/* Updated: 2017/09/29 18:01:12 by fmessina ### ########.fr */
/* Updated: 2017/11/12 21:56:36 by fmessina ### ########.fr */
/* */
/* ************************************************************************** */
View
@@ -81,10 +81,10 @@ void load_scene(t_env *e)
//plane 1
ft_bzero(&e->planes[0], sizeof(t_plane));
e->planes[0].pos.x = 0;
e->planes[0].pos.y = 0;
e->planes[0].pos.y = -10;
e->planes[0].pos.z = 0;
e->planes[0].normale.x = 0;
e->planes[0].normale.y = -1;
e->planes[0].normale.y = 1;
e->planes[0].normale.z = 0;
e->planes[0].color = 0x00ff0000;
@@ -186,7 +186,7 @@ void frame_init(t_env *e)
load_scene(e);
}
void init(t_env *e)
void init(t_env *e, int ac, char *av)
{
ft_bzero(e, sizeof(t_env));
e->debug = DBUG;
@@ -199,16 +199,17 @@ void init(t_env *e)
e->gpu = IS_GPU;
e->param.bloom = 1.80;
e->run = 1;
if (!(e->xml = malloc(sizeof(t_xml))))
s_error("\x1b[2;31mCan't initialize the xml buffer\x1b[0m", e);
if (!(e->mlx = mlx_init()))
s_error("\x1b[2;31mError can't initialize minilibx\x1b[0m", e);
if (!(e->win = mlx_new_window(e->mlx, e->win_w, e->win_h, "RTv1")))
s_error("\x1b[2;31mError minilibx window creation failed\x1b[0m", e);
frame_init(e);
get_file(e, ac, av);
if (opencl_init(e) != 0)
{
e->gpu = 0;
opencl_init(e);
}
set_hooks(e);
}
View
@@ -7,7 +7,7 @@ int main(int ac, char **av)
(ac > 2 ? print_usage() : 0);
if (!(e = malloc(sizeof(t_env))))
s_error("\x1b[2;31mCan't initialize RTv1 environment\x1b[0m", NULL);
get_file(e, ac, av[1]);
init(e);
init(e, ac, av[1]);
set_hooks(e);
return (0);
}
View
@@ -7,10 +7,10 @@ void print_usage()
void flush(t_env *e)
{
if (e->scene)
free(e->scene);
if (e->kernel_src)
free(e->kernel_src);
if (e->xml)
free(e->xml);
if (e->frame && e->frame->ptr)
mlx_destroy_image(e->mlx, e->frame->ptr);
if (e->frame)
View
@@ -1,85 +1,145 @@
#include "rtv1.h"
static void check_char(t_env *e, char c)
int check_char(char c)
{
if (c != '"' && c != 10 && c != ',' && c != ' ' && c != '<' && c != '>' && \
c != '.' && c != '/' && c != '-' && c != '=' && c != '!' && \
c != '.' && c != '/' && c != '-' && c != '=' && c != '!' && c != 9 && \
ft_isdigit(c) != 1 && ft_isalphalowercase(c) != 1)
s_error("\x1b[2;31mError, illegal character found in scene\x1b[0m", e);
return (1);
else
return (0);
}
static void check_file(t_env *e)
int check_valid_char(t_env *e, char *buf)
{
int i;
t_xml xml;
ft_bzero((void *)&xml, sizeof(t_xml));
int i;
i = -1;
while (e->scene[++i])
while (buf[++i])
{
if (check_char(buf[i]) != 0)
return (1);
(buf[i] == '<' ? e->xml->lbra++ : 0);
(buf[i] == '>' ? e->xml->rbra++ : 0);
(buf[i] == '/' ? e->xml->slas++ : 0);
(buf[i] == '"' ? e->xml->dquo++ : 0);
(buf[i] == '!' ? e->xml->excl++ : 0);
}
return (0);
}
char *check_comment_active(t_env *e, char *buf)
{
int i;
i = 0;
if (ft_strstr(buf, "<!--") != NULL)
s_error("\x1b[2;31mError, nested comment\x1b[0m", e);
while (buf[i])
{
check_char(e, e->scene[i]);
(e->scene[i] == '<' ? xml.lbra++ : 0);
(e->scene[i] == '>' ? xml.rbra++ : 0);
(e->scene[i] == '/' ? xml.slas++ : 0);
(e->scene[i] == '"' ? xml.dquo++ : 0);
(e->scene[i] == '!' ? xml.excl++ : 0);
if (buf[i] == '-' && buf[i + 1] == '-' && buf[i + 2] == '>')
{
ft_memset(&buf[i], ' ', 3);
e->xml->comment = 0;
break ;
}
buf[i] = ' ';
i++;
}
if ((xml.lbra != xml.rbra) || (xml.slas != xml.rbra - 1 - xml.excl) || \
(xml.dquo % 2 != 0))
s_error("\x1b[2;31mError, invalid scene content\x1b[0m", e);
return (ft_strtrim_free(buf));
}
static void read_file(t_env *e)
char *check_comment_inactive(t_env *e, char *buf)
{
int i;
int status;
char comment;
char *buf;
char *cur;
int i;
i = 0;
comment = 0;
if ((cur = ft_strstr(buf, "<!--")) != NULL)
{
ft_memset(cur, ' ', 4);
e->xml->comment = 1;
while (cur[i])
{
if (cur[i] == '<' && cur[i + 1] == '!' && cur[i + 2] == '-' && cur[i + 3] == '-')
s_error("\x1b[2;31mError, nested comment\x1b[0m", e);
if (cur[i] == '-' && cur[i + 1] == '-' && cur[i + 2] == '>')
{
ft_memset(&cur[i], ' ', 3);
e->xml->comment = 0;
break ;
}
cur[i] = ' ';
i++;
}
}
return (ft_strtrim_free(buf));
}
char *check_buf(t_env *e, char *buf)
{
if (e->xml->comment == 0)
buf = check_comment_inactive(e, buf);
if (e->xml->comment == 1)
buf = check_comment_active(e, buf);
// printf("%s\n", buf);
if (e->xml->comment == 0)
if (check_valid_char(e, buf) != 0)
s_error("\x1b[2;31mError, invalid scene content\x1b[0m", e);
return(buf);
}
int check_xml_header(char *buf)
{
if (ft_strcmp(buf, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>") != 0)
return (0);
else
return (1);
}
void read_file(t_env *e)
{
int count;
int status;
char *buf;
count = 0;
status = 1;
while (status == 1)
{
status = get_next_line(e->scene_fd, &buf);
status = get_next_line(e->xml->scene_fd, &buf);
if (status == -1)
p_error("\x1b[2;31mError reading scene\x1b[0m", e);
else if (status == 0)
break ;
else if (comment == 1 && ft_strstr(buf, "-->") == NULL)
free(buf);
else if (comment == 1 && ft_strstr(buf, "-->") != NULL)
{
comment = 0;
free(buf);
}
else if (comment == 0 && ft_strstr(buf, "<!--") != NULL)
{
comment = 1;
free(buf);
}
else
e->scene = ft_strjoin_free(e->scene, \
ft_strtrim_free(ft_strjoin_frs1(buf, "\n")));
i++;
{
if (count == 0 && check_xml_header(buf) != 1)
s_error("\x1b[2;31mError missing scene header\x1b[0m", e);
if (count > 0)
buf = check_buf(e, buf);
e->xml->scene = ft_strjoin_free(e->xml->scene, \
ft_strtrim(ft_strjoin_frs1(buf, " ")));
count++;
}
}
printf("SCENE = \n%s\n", e->scene);
close(e->xml->scene_fd);
printf("SCENE = \n%s\n", e->xml->scene);
}
void get_file(t_env *e, int ac, char *av)
void get_file(t_env *e, int ac, char *av)
{
if (ac > 2)
s_error("\x1b[2;31mError, too many arguments\x1b[0m", e);
else if (ac == 2)
e->scene = ft_strdup(av);
e->xml->scene = ft_strdup(av);
else
{
ft_putendl("\x1b[2;33mNo scene file specified, loading default\x1b[0m");
e->scene = ft_strdup("./default.xml");
e->xml->scene = ft_strdup("./default.xml");
}
if ((e->scene_fd = open(e->scene, O_RDONLY)) < 0)
if ((e->xml->scene_fd = open(e->xml->scene, O_RDONLY)) < 0)
p_error("\x1b[2;31mCan't open scene file\x1b[0m", e);
ft_bzero((void*)e->scene, ft_strlen(e->scene));
ft_bzero((void*)e->xml->scene, ft_strlen(e->xml->scene));
e->xml->comment = 0;
read_file(e);
check_file(e);
}
Oops, something went wrong.

0 comments on commit 15613d4

Please sign in to comment.