/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* parse.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: tomoron +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/07/29 11:14:01 by tomoron #+# #+# */ /* Updated: 2023/07/30 20:22:03 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ #include "header.h" int ft_calc_part_len(char *str, int mode) { int res; res = 0; if (mode == 1) { while (*str >= '0' && *str <= '9') { res++; str++; } } if (mode == 2) { while (*str >= 32 && *str <= 126) { res++; while (*str == ' ') str++; str++; } } return (res + 1); } int ft_copy_nb(char *res, char *file, int k) { int j; j = 0; while (file[k] >= '0' && file[k] <= '9') { res[j] = file[k]; k++; j++; } res[j] = 0; while (file[k] == ' ') k++; if (file[k] != ':' || j == 0) return (0); k++; while (file[k] == ' ') k++; return (k); } int ft_copy_value(char *res, char *file, int k) { int j; j = 0; while (file[k] >= 32 && file[k] <= 126) { res[j] = file[k]; if (file[k] == ' ') { while (file[k] == ' ') k++; if (file[k] == '\0' || file[k] == '\n') j--; k--; } k++; j++; } res[j] = 0; if ((file[k] != '\n' && file[k] != '\0') || j == 0) return (0); while (file[k] == '\n') k++; return (k); } int ft_fill_dict(char **res, char *file, int i, int k) { while (file[k] == ' ') k++; res[i] = malloc(ft_calc_part_len(file + k, 1) * sizeof(char)); if (!res[i]) return (ft_error(res, file, i - 1)); k = ft_copy_nb(res[i], file, k); if (!k) return (ft_error(res, file, i)); i++; res[i] = malloc(ft_calc_part_len(file + k, 2) * sizeof(char)); if (!res[i]) return (ft_error(res, file, i - 1)); k = ft_copy_value(res[i], file, k); if (!k) return (ft_error(res, file, i)); return (k); } char **ft_parse_dict(char *filename) { char **res; char *file; int i; int k; file = ft_read_file(filename); if (!file) return (0); res = malloc(ft_count_parts(file) * sizeof(char *)); if (!res) return (0); k = 0; while (file[k] == '\n') k++; i = 0; while (file[k]) { k = ft_fill_dict(res, file, i, k); if (!k) return (0); i += 2; } res[i] = 0; free(file); return (res); } /*int main(void) { char **test; int i; i = 0; test = ft_parse_dict("lol.dict"); while (test && test[i]) { printf("%s, ", test[i]); fflush(stdout); i++; } return (0); }*/