从 var 中读取数据并将其传递给 C 中的另一个的问题

Problem in reading data from a var and pass it to other one in C

我正在尝试使用 C 创建 3D 游戏。 但我在以下代码中遇到问题

解释代码

我有一个名为1.cub的文件(锁定代码),这个文件有地图。 使用函数 read_cf_color 我从 1.cub 文件中读取了地板和天空的颜色,并将地板颜色存储在名为 mlx->floor_color 的变量中,并将天花板颜色存储在名为 mlx->sky_color 的变量中]. P.S:使用其他一些函数,我从 1.cub 读取所有数据并将其存储在名为 mlx->lines.

的 var 中

parse_parameters : 扫描所有 mlx->lines 中的数据(1.cub 文件)并将每一行发送到 choose_param.

choose_param:从 parse_parameters 获取行并用正确的函数匹配每个 c

read_cf_color:该函数获取floor/ceiling的颜色。并将其从 char * 转换为 long int.

ft_isfloor:从上一个函数中获取颜色,并将其赋予正确的var(mlx->floor_color/mlx->sky->color).

例如:

parse_parameters pass this line F 100 , 100 , 214 to choose_param.

Then choose_param pass The F To char c in read_cf_color and the rest 100 , 100 , 214 to char *s.

the function try to make this 100 , 100 , 214 look like this 100100214 and store it in long int color

int     choose_param(char c, char *str, t_mlx *mlx)
{
    while (*str == ' ')
        str++;
    if (c == 'R' && *(str + 1) == ' ')
        read_resolution(str + 1, mlx);
    else if (c == 'N' && *(str + 1) == 'O' && *(str + 2) == ' ')
        read_txt(1, str + 2, mlx);
    else if (c == 'S' && *(str + 1) == 'O' && *(str + 2) == ' ')
        read_txt(2, str + 2, mlx);
    else if (c == 'W' && *(str + 1) == 'E' && *(str + 2) == ' ')
        read_txt(4, str + 2, mlx);
    else if (c == 'E' && *(str + 1) == 'A' && *(str + 2) == ' ')
        read_txt(3, str + 2, mlx);
    else if (c == 'F' && *(str + 1) == ' ')
        read_cf_color(str + 1, mlx, 'f');
    else if (c == 'C' && *(str + 1) == ' ')
        read_cf_color(str + 1, mlx, 'c');
    else if (c == 'S' && *(str + 1) == ' ')
        read_txt(5, str + 1, mlx);
    else if (c == '1' || c == '0' || c == '2')
        return (0);
    else if (c)
        ft_put_error("NON-EMPTY LINE WITH WRONG IDENTIFIER\n", mlx);
    return (1);
}

char    **parse_parameters(t_mlx *mlx, char **lines)
{
    int i;
    int j;
    int map;

    i = 0;
    map = 0;
    while (lines[i])
    {
        j = 0;
        while (lines[i][j] == ' ')
            j++;
        if (!choose_param(lines[i][j], lines[i], mlx))
        {
            return (&lines[i]);
        }
        i++;
    }
    ft_put_error("NO MAP FOUND\n", mlx);
    return (lines);
}
void    ft_isfloor(char *s, t_mlx *mlx, char type, char *stor)
{
    if (type == 'f')
    {
        ft_printf("The rest of The floor:\t|%s|\n",s);
        extra_param(s, mlx, "Too many Floor rgb inputs");
        mlx->floor_color = ft_atoi(stor);
        ft_printf("The floor Value: |%d|\n",mlx->floor_color);
        if (mlx->floor_color < 0)
            ft_put_error("Wrong values for floor color\n", mlx);
        mlx->floor_done = 1;
    }
    else if (type == 'c')
    {
        ft_printf("The rest of The ceiling\t\n|%s|\n",s);
        extra_param(s, mlx, "Too many ceiling rgb inputs");
        mlx->sky_color = ft_atoi(stor);
        ft_printf("The ceiling Value: |%d|\n",mlx->sky_color);
        if (mlx->floor_color < 0)
            ft_put_error("Wrong values for ceiling color\n", mlx);
        mlx->sky_done = 1;
    }
}

void    read_cf_color(char *s, t_mlx *mlx, char type)
{
    long int    color;
    char        *stor;
    int         i;

    i = 3;
    
    color = 0;
    if (mlx->floor_done && type == 'f')
        ft_put_error("Multiple floor color inputs\n", mlx);
    if (mlx->sky_done && type == 'c')
        ft_put_error("Multiple ceiling color inputs\n", mlx);
    stor = (char *)malloc(sizeof(char) * 10);
    while (i > 0)
    {
        ft_printf("s = |%s|\n",s);
        while (*s == ' ')
            s++;
        ft_printf("s = |%s|\n",s);
        color = ft_atoi(s);
        s += ft_intsize(color);
        stor = ft_strjoin(stor, ft_itoa(color));
        ft_printf("color = |%d|\nstor = |%s|\n---------\n",color, stor);
        while (*s == ' ')
            s++;
        ft_printf("s = |%s|\n",s);
        if (*(s++) != ',' && i > 1)
            ft_put_error("Wrong floor color input\n", mlx);
        i--;
    }
    ft_printf("\tThe final color |%s|\n",stor);
    ft_isfloor(s, mlx, type, stor);
    free(stor);
}

文件1.cub:

R 1000 750
F 100    ,   100   ,   214
C 135,    197    ,214

EA ./textures/cat4.xpm
NO ./textures/cat1.xpm
SO ./textures/cat2.xpm
WE ./textures/cat3.xpm

S ./textures/barrel.xpm

1000000001
11111111111      100000000100000010111
10000000010000
1111101111
1111101111111111111111111111111111111111111
010000000100000110000000000000000000000001
01000000010000011000000000000000001001000111111111111111111111111111111                     
1111110100000001100000000000000000110000000000000000000000010000001
1000000000000001100000000001111110101111111111111111111110000111101
1111110000000001100000010000000000100001100000100000000000000000001
10000010000000011000000000000100001011000000001111111111111111111111
10000000000       0000000000010000000000010110000000001
100000100000000110000000000000000010110000000111
111111000000000110000000000000010010000000001
100011100000W00000000001111110000010000000001      
1000000000000001100000000   000000000100000000001
1000000000000001100000000000000000100000000001
100000000000000110000000002000000010010000001
10000000000000011000000000000000001000000001
10000000000000011000000000000000001000000001
1111111111111111111111111111111111111111111
1111111111111111111111111111111111111111111

当我编译所有这些时,我得到了这个:

图片中:

--> 地板颜色存储后 free(stor) 开始存储天花板颜色时,var stor 中的旧值保留并添加新数据:

--> 将所有数据存储在 ss = || 之后,它会得到一个新数据,如图所示:

希望你明白我想说的意思

问题出在最后一个 if 语句中:

if (*(s++) != ',' && i > 1)
            ft_put_error("Wrong floor color input\n", mlx);

我阅读的内容超出了 [=11=],所以这一切都发生了。

stor分配了2次:

The first time in stor = (char *)malloc(sizeof(char) * 10);

The second time when I call ft_strjoin.