char *ft_strtrim(char const *s) { int pos_e; t_bool pos_t; size_t pos_s; char *v_str; pos_e = 0; pos_s = 0; pos_t = FALSE; v_str = ft_strnew(ft_strlen(s)); while (pos_s < ft_strlen(s)) { if (!pos_t && ft_iswhite(s[pos_s])) pos_s++; else { pos_t = TRUE; v_str[pos_e++] = s[pos_s++]; } } while (ft_iswhite(v_str[--pos_e])) v_str[pos_e] = '\0'; return (v_str); }
enum e_parse_state get_state(const char *line) { if (ft_strnequ(line, NAME_CMD_STRING, NAME_CMD_LEN) && ft_iswhite(line[NAME_CMD_LEN])) return (_PARSE_NAME); else if (ft_strnequ(line, COMMENT_CMD_STRING, COMMENT_CMD_LEN) && ft_iswhite(line[COMMENT_CMD_LEN])) return (_PARSE_COMMENT); else if (is_labelled(line)) return (_PARSE_LABEL); else return (_PARSE_INSTRUCTION); return (_PARSE_ERROR); }
char *ft_strtrim(char const *s) { size_t i; size_t start; size_t end; char *dest; if (s == NULL) return (NULL); i = 0; while (ft_iswhite(s[i]) && s[i]) i++; start = i; end = ft_end((char *)s, start); dest = (char *)malloc(sizeof(char)); if (start != end || (start == end && start != (size_t)ft_strlen(s))) { if (!(dest == (char *)malloc((end - start + 2) * sizeof(char)))) return (NULL); ft_strncpy(dest, s + start, end - start + 1); dest[end - start + 1] = 0; } else if (start == end && start == (size_t)ft_strlen(s)) *dest = '\0'; return (dest); }
static size_t count_spaces(char const *s) { size_t i; i = 0; while (ft_iswhite(s[i])) i++; return (i); }
static size_t count_spaces_return(char const *s, size_t len) { if (len) { len--; while (ft_iswhite(s[len]) && len > 0) len--; } return (len); }
char *ft_ctos(char c) { char *s; if ((s = malloc(2)) == NULL) return (NULL); s[0] = c >= 0 && c < 31 && !ft_iswhite(c) ? 0 : c; s[1] = '\0'; return (s); }
size_t ft_end(char *s, size_t i) { size_t end; end = i; while (s[i]) { if (!ft_iswhite(s[i])) end = i; i++; } return (end); }
static int is_empty(char *s) { int i; i = 0; while (s[i]) { if (ft_iswhite(s[i])) return (1); ++i; } return (0); }
static t_bool is_labelled(const char *line) { size_t i; i = 0; while (line[i] && !ft_iswhite(line[i])) { if (i >= 1 && line[i] == LABEL_CHAR) return (_TRUE); if (ft_strchr(LABEL_CHARS, line[i]) == NULL) return (_FALSE); i++; } return (_FALSE); }
double ft_atod(const char *str) { double nb; double part; double sign; while (ft_iswhite(*str)) str++; sign = (*str == '-') ? -1.0 : 1.0; str = (*str == '-' || *str == '+') ? str + 1 : str; nb = 0.0; while (*str >= '0' && *str <= '9') nb = nb * 10 + (*(str++) - '0'); if (*str == '.' || *str == ',') { while (*(++str) >= '0' && *str <= '9') part = 0.0; while (*(--str) >= '0' && *str <= '9') part = (part + (*str - '0')) / 10.0; nb += part; } return (nb * sign); }