int get_next_line(int fd, char **line) { static char **memory = NULL; register char *tmp; register char *ptr; register int value; if (fd == 0) return (-1); if ((value = ft_init(&memory, fd, &line)) <= 0) return (value); ptr = ft_strchr((memory[fd]), '\n'); if (ptr + 1 == '\0') *ptr = '\0'; if (!ptr || ptr + 1 == '\0') { *line = (memory[fd]); (memory[fd]) = NULL; return ((*(*line)) ? 1 : 0); } if (!(*line = ft_strcdup((memory[fd]), '\n')) || !(tmp = ft_strdup(ptr + 1))) return (-1); free((memory[fd])); (memory[fd]) = tmp; return (1); }
t_return parse(int fd, t_header *header, t_instruction **instructions, t_label **labels) { char *line; enum e_parse_state state; char *tmp; static t_parse_fct parse_line[_NB_PARSE_STATE] = { &set_header_name, &set_header_comment, &add_label, &add_instruction }; line = NULL; while (get_next_line(fd, &line) > 0) { tmp = ft_strcdup(line, COMMENT_CHAR); free(line); line = ft_strtrim(tmp); if (line[0] && (state = get_state(line)) != _PARSE_ERROR) { if (parse_line[state](line, header, instructions, labels) == _ERR) return (PERROR("Parsing Error.")); } free(line); } return (_SUCCESS); }
int get_next_line(int const fd, char **line) { static char *buf; char *tmp; int read_bytes; if (BUFF_SIZE > MAX_SIZE_BUFFER || BUFF_SIZE <= 0 || fd == 1) return (-1); if ((read_bytes = read_line(&buf, fd)) == -1) return (-1); *line = ft_strcdup(buf, '\n'); tmp = buf; buf = ft_strdup(buf + ft_strclen(buf, '\n') + 1); free(tmp); return (read_bytes == 0) ? 0 : 1; }
static char *change_into_number(char *av) { char *var_name; int number; if (!(var_name = ft_strcdup(av, '='))) return (av); if (!(ft_strcmp(var_name, "SHLVL"))) { if (!(number = ft_atoi(ft_strchr(av, '=') + 1))) { free(var_name); free(av); ft_putendl_fd("Error : SHLVL must be a number", 2); return (NULL); } } free(var_name); return (av); }
int check_if_in_env(char *av, char **env) { int x; char *var_name; x = 0; while (env[x]) { if (!(var_name = ft_strcdup(env[x], '='))) return (-1); if ((ft_strcmp(var_name, av)) == -61) { free(var_name); return (x); } free(var_name); x++; } return (-1); }
int gnl_copy(char **line, char *str, int ret) { int len; if (*line != NULL) ft_strdel(line); if (ft_strchr(str, '\n')) { *line = ft_strcdup(str, '\n'); len = ft_strlen(*line); ft_memmove(str, str + len + 1, ft_strlen(str) - len); } else if (ret == 0) { *line = ft_strdup(str); if (str) ft_strdel(&str); return (1); } if (str == NULL && ret <= 0) return (0); return (1); }