int check_file_name(char *file_name)
{
int i;
if (!file_name || ft_strlen(file_name) > 4)
return (-1);
i = ft_strlen(file_name) - 1;
if (file_name[i] == 'd' && file_name[i - 1] == 'u' &&
file_name[i - 2] == 'c' && file_name[i - 3] == '.')
return (1);
}
*.cub).cub로 끝나는지 확인한다.int parse_line(char *line, t_info *info, int gnl_return)
{
t_map *map;
int i;
int line_length;
int result;
line_length = ft_strlen(line);
if (line_length == 0)
return (0);
i = 0;
while (line[i] != '\0')
{
if (is_space(line[i]))
i++;
else if (is_type_identifier(line[i], line[i + 1], line))
break ;
else if (is_map_character(line[i], gnl_return))
i += save_map(line, gnl_return);
else
return (-1);
}
return (1);
}
int is_space(char c)
{
if ((c >= 9 && c <= 13) || c == 32)
return (1);
else
return (-1);
}
int is_type_identifier(char a, char b, char *line)
{
if ((a == 'R' || a == 'S' || a == 'F' || a == 'C') && is_space(b))
{
if (a == 'R')
config_resolution(line + 1);
else if (a == 'S')
config_path(4, line + 1);
else
config_color(a, line + 1);
}
else if (a == 'N' && b == 'O')
config_path(0, line + 2);
else if (a == 'S' && b == 'O')
config_path(1, line + 2);
else if (a == 'W' && b == 'E')
config_path(2, line + 2);
else if (a == 'E' && b == 'A')
config_path(3, line + 2);
else
return (-1);
return (0);
}
int is_map_character(char c)
{
if (c == '0' || c == '1' || c == '2' || c == 'N' || c == 'S' ||
c == 'W' || c == 'E')
return (1);
return (0);
}
int save_map(char *line, int gnl_return)
{
static char **save;
char *tmp;
int line_len;
line_len = ft_strlen(line);
tmp = *save;
*save = ft_strjoin(*save, line);
free(tmp);
if (gnl_return != 0) //is_not_eof
{
tmp = *save;
*save = ft_strjoin(*save, "\n");
free(tmp);
}
free(line);
return (line_len);
}
map에 해당하는 line을 character 타입 배열인 save 변수에 저장한다.
libft의 ft_strjoin() 함수를 이용해 map line들을 연결한다.
메모리 누수를 막기 위해 tmp 변수를 선언한다. save 포인터를 strjoin()의 결과값으로 덮어씌우기 전에
tmp에 할당해 메모리 주소를 잃어버려 접근이 불가능해지는 것을 막는다.
line 연결이 끝나면 tmp 변수는 메모리에서 해제해준다.
get_next_line()에서 반환되는 리턴값 gnl_return을 인자로 받는다.
- gnl_return이 0이 아니면, EOF(End of File)이 아니므로 각 map line 끝을 개행문자 \n로 마무리해준다.
- gnl_return이 0이면(=EOF일 때) 개행문자로 마무리하지 않아도 된다.
사용이 끝난 line 변수도 메모리에서 해제한다.
리턴값은 line의 길이인 line_len이다.