cparser_result_t cparser_line_delete (cparser_t *parser) { cparser_line_t *line; int n; if (!VALID_PARSER(parser)) { return CPARSER_ERR_INVALID_PARAMS; } line = &parser->lines[parser->cur_line]; assert(line->current <= line->last); if (!line->last || !line->current) { /* Line is empty or we're at the beginning of the line */ return CPARSER_ERR_NOT_EXIST; } /* Move all character after current position back by one */ for (n = line->current; n < line->last; n++) { line->buf[n-1] = line->buf[n]; } line->current--; line->last--; line->buf[line->last] = '\0'; /* Update the display */ parser->cfg.printc(parser, '\b'); parser->cfg.prints(parser, LINE_CURRENT(line)); parser->cfg.prints(parser, " \b"); for (n = line->current; n < line->last; n++) { parser->cfg.printc(parser, '\b'); } return CPARSER_OK; }
cparser_result_t cparser_line_insert(cparser_t *parser, char ch) { int n; cparser_line_t *line; if (!VALID_PARSER(parser) || !ch) { return CPARSER_ERR_INVALID_PARAMS; } line = &parser->lines[parser->cur_line]; if (CPARSER_MAX_LINE_SIZE <= line->last) { return CPARSER_ERR_OUT_OF_RES; } /* * Move all characters from current to last back by 1 */ for (n = line->last; n > line->current; n--) { line->buf[n] = line->buf[n - 1]; } line->buf[++line->last] = '\0'; /* * Insert the new character and update the line display. We do not * have full curse support here. Instead, we simply assume all * characters are on the same line and use backspace to move the * cursor. */ line->buf[line->current] = ch; parser->cfg.printc(parser, ch); line->current++; /* update current position */ parser->cfg.prints(parser, LINE_CURRENT(line)); /* * Move cursor back to the current position */ for (n = line->current; n < line->last; n++) { parser->cfg.printc(parser, '\b'); } return CPARSER_OK; }