char *get_str_from_line_to_line(Text *text, size_t from, size_t to) { size_t pos_line = 1; size_t pos_end = 1; size_t i; size_t j; char *buf; for (i = 0; i < text->size; ++i) { if (pos_line == from) { break; } if (i == text->gap_start) { i += GAPSIZE(text) - 1; continue; } if (text->buf[i] == '\n') { ++pos_line; ++pos_end; } } for (j = i; j < text->size; ++j) { if (pos_end == to+1) { break; } if (j == text->gap_start) { j += GAPSIZE(text) - 1; continue; } if (text->buf[j] == '\n') { ++pos_end; } } buf = calloc(1, j - i + 1); if (!buf) return NULL; if (i <= text->gap_start) { size_t end = MIN(text->gap_start, j); size_t len = end - i; size_t send = MIN(text->size, j); memcpy(buf, text->buf + i, len); if (send > text->text_start) { size_t slen = send - text->text_start; memcpy(buf + len, text->buf + text->text_start, slen); } } else { size_t end = MIN(text->size, j); size_t len = end - text->text_start - i; memcpy(buf, text->buf + i, len); } return buf; }
char *get_chars_rowcol_to_rowcol(Text *text, int rowa, int cola, int rowb, int colb) { int r = 0, c = 0; size_t i, bytes; char *ret; size_t start, end; for (i = 0; i < text->size; ++i) { if (i == text->gap_start) { i += GAPSIZE(text) - 1; continue; } if (r >= rowa && c == cola) break; if (text->buf[i] == '\n') { if (r == rowa && c == cola - 1) break; ++r; c = 0; } else if (text->buf[i] == '\t') { c += 8; } else { bytes = more_bytes_utf8[(unsigned int)(unsigned char)text->buf[i]]; i += bytes; ++c; } } start = i; for (; i < text->size; ++i) { if (i == text->gap_start) { i += GAPSIZE(text) - 1; continue; } if (r >= rowb && c == colb) break; if (text->buf[i] == '\n') { if (r == rowb && c == colb - 1) break; ++r; c = 0; } else if (text->buf[i] == '\t') { c += 8; } else { bytes = more_bytes_utf8[(unsigned int)(unsigned char)text->buf[i]]; i += bytes; ++c; } } end = i; ret = calloc(1, end - start + 1); text_memcpy_from_offsets(ret, 0, text, start, end); return ret; // TODO handle unicode }
int resize_gap(Text *text, size_t new_gap) { size_t gap = GAPSIZE(text); size_t expand_by; size_t old_size, new_size; size_t old_start; char *old_buf; // resize_gap can't downsize if (new_gap <= gap) return 0; expand_by = new_gap - gap; old_size = text->size; new_size = text->size + expand_by; text->size = new_size; old_start = text->text_start; text->text_start += expand_by; old_buf = text->buf; text->buf = (char*)calloc(1, new_size); if (!text->buf) { text->buf = old_buf; return -1; } memcpy(text->buf, old_buf, text->gap_start); memcpy(text->buf + text->text_start, old_buf + old_start, old_size - old_start); free(old_buf); return 0; }
char *get_chars_rowcol(Text *text, int row, int col, int length, int forwards) { int r = 0, c = 0; size_t i, bytes; char *ret; for (i = 0; i < text->size; ++i) { if (i == text->gap_start) { i += GAPSIZE(text) - 1; continue; } if (r >= row && c == col) break; if (text->buf[i] == '\n') { if (r == row && c == col - 1) break; ++r; c = 0; } else if (text->buf[i] == '\t') { c += 8;// - (c % 8); } else { // Handle unicode bytes = more_bytes_utf8[(unsigned int)(unsigned char)text->buf[i]]; i += bytes; ++c; } } // TODO handle unicode ret = calloc(1, length + 1); if (forwards) text_memcpy_from(ret, 0, text, i, length); else text_memcpy_from(ret, 0, text, text_offset(text, i, -length), length); return ret; }
char *get_ch_rowcol(Text *text, int row, int col) { int r = 0, c = 0; size_t i, bytes; char *ret; for (i = 0; i < text->size; ++i) { if (i == text->gap_start) { i += GAPSIZE(text) - 1; continue; } if (r >= row && c == col) break; if (text->buf[i] == '\n') { if (r == row && c == col - 1) break; ++r; c = 0; } else if (text->buf[i] == '\t') { c += 8;// - (c % 8); if (r == row && c >= col) { if (c == col) ++i; break; } } else { // Handle unicode bytes = more_bytes_utf8[(unsigned int)(unsigned char)text->buf[i]]; i += bytes; ++c; } } bytes = more_bytes_utf8[(unsigned int)(unsigned char)text->buf[i]]; ret = calloc(1, bytes + 2); memcpy(ret, text->buf + i, bytes+1); return ret; }
size_t text_offset(Text *text, size_t start, int offset) { if (start >= text->text_start && (int)start + offset < text->text_start) { return start + offset - GAPSIZE(text); } return start + offset; }
char *get_str_from_line(Text *text, size_t line) { size_t pos_line = 1; size_t i; char *buf; for (i = 0; i < text->size; ++i) { if (pos_line == line) { break; } if (i == text->gap_start) { i += GAPSIZE(text) - 1; continue; } if (text->buf[i] == '\n') { ++pos_line; } } buf = calloc(1, NBYTES(text) - i + 1); if (!buf) return NULL; if (i <= text->gap_start) { memcpy(buf, text->buf + i, text->gap_start - i); memcpy(buf + text->gap_start - i, text->buf + text->text_start, text->size - text->text_start); } else { memcpy(buf, text->buf + i, text->size - text->text_start - i); } return buf; }
int add_character(Text *text, char c) { size_t freespace = GAPSIZE(text); if (freespace <= 1) if (0 != resize_gap(text, RESIZE_BY)) return -1; text->buf[text->gap_start++] = c; return 0; }
int add_string(Text *text, char *buf, size_t len) { size_t freespace = GAPSIZE(text); if (freespace < len + 1) if (0 != resize_gap(text, RESIZE_BY + len)) return -1; memcpy(text->buf + text->gap_start, buf, len); text->gap_start += len; return 0; }
int move_cursor_to_end(Text *text) { size_t gap = GAPSIZE(text); char *old_buf = text->buf; text->buf = (char*)calloc(1, text->size + 1); if (!text->buf) { text->buf = old_buf; return -1; } memcpy(text->buf, old_buf, text->gap_start); memcpy(text->buf + text->gap_start, old_buf + text->text_start, text->size - text->text_start); text->gap_start = text->size - gap; text->text_start = text->size; free(old_buf); return 0; }
static int compute_tot_size(struct params *fd, int chunksize, int gap, int tailsize) { int i, nr; fd->nssect = 0; fd->actual_interleave = 1; for(i= 0; i < MAX_SIZECODE; i++){ nr = nrSectorsForSize(fd, i); fd->nssect += chunks_in_sect(fd, i, gap, chunksize) * nr; if(nr && GAPSIZE(i) < 34) fd->actual_interleave = 2; } if (tailsize >= 0) return (fd->nssect - chunks_in_sect(fd, tailsize, gap, chunksize)) * chunksize + SSIZE(tailsize); else return fd->nssect * chunksize; }
int get_rowcoloffset_of_chars(Text *text, char *ch, int *row, int *col, int *offset, int forwards) { int r, c; int i, j, mo,bytes, matched, length; r = 0, c = 0; get_cursor_rowcol(text, &r, &c); *offset = 0; length = strlen(ch); if (forwards) { for (i = text->text_start; i + length < text->size; ++i) { matched = 1; for (j = 0; j < length; ++j) { if (text->buf[i + j] != ch[j]) { matched = 0; break; } } if (matched) { goto done; } if (text->buf[i] == '\n') { ++r; c = 0; } else if (text->buf[i] == '\t') { c += 8; } else { bytes = more_bytes_utf8[(unsigned int)(unsigned char)text->buf[i]]; i += bytes; ++c; } ++(*offset); } } else { for (i = ((int)text->gap_start)-1; i >= 0; --i) { matched = 1; mo = 0; for (j = 0; j < length; ++j) { if (i + j == text->gap_start) mo = GAPSIZE(text); if (text->buf[i + j + mo] != ch[j]) { matched = 0; break; } } if (matched) { c = -1; for (; i >= 0; --i) { if (text->buf[i] == '\n') { break; } else if (text->buf[i] == '\t') { c += 8; } else { bytes = more_bytes_utf8[(unsigned int)(unsigned char)text->buf[i]]; i -= bytes; ++c; } } goto done; } if (text->buf[i] == '\n') { --r; } else { bytes = more_bytes_utf8[(unsigned int)(unsigned char)text->buf[i]]; i -= bytes; } ++(*offset); } } return -1; done: *row = r; *col = c; return 0; }