From 380f3e1ff7e733cf5ec55f83fa61661a373ba03e Mon Sep 17 00:00:00 2001 From: FantasyPvP <80643031+FantasyPvP@users.noreply.github.com> Date: Sat, 9 Nov 2024 17:29:01 +0000 Subject: [PATCH] pretty much working, need to fix horizontal scrolling & file saving --- final/binary | Bin 22336 -> 22464 bytes final/editor.c | 56 ++++++++-- final/log.txt | 259 ++++++++++++++++++++++++++++++++++++++++++++++- final/somefile.c | 221 ---------------------------------------- 4 files changed, 308 insertions(+), 228 deletions(-) delete mode 100644 final/somefile.c diff --git a/final/binary b/final/binary index 71919e902a76e6ff6095bf02b4cab27eb6d275d5..75f6145aa0931100d4cefcdc7438e28e8ac7190a 100755 GIT binary patch delta 6741 zcmZ`;4OCTCzW<$b?~xB7K9vtaE+~eXqA4jU25H@tA9jt}SjnVI0;+)Of4cY}~?Mi?sOksLA}ED34Er3so8r z4@ei1*RPKiYBo8zw|71>0&rui1J|anj{;n%ckB}K6%I{2t|fN@h`kM*s$1 zb71z|)tkbN%%eRGDb?;O$+nt*yjWzqHH3cKD5(PiILKOVfWg=!93yOgK zBJ;x3yM#j9m@WciwFD1eCbG;AnQde{7-4{Awldp@D+1mV8QU)cC%BhLh2}eP`w+LU zX}hnfb~^K$4a{nJN3aCfL;=h;8<<`BEVImZW>p55ZSG=r*jd4B^Bv|N zwNHBdxzPKdFiONRQDksQ1G5e?tDRZR%=#pFOXNRO>bs_u*~aQQ%E%{`*A))9NP5^7 zx6eGattoC>wFW#|*LIr*#BKX6t^qr;6(vGYb|fo6*MPcF^>uC0aoc_gpuVncjCWSY z_W*3O%{N)l>-&wkU!DeFH8Fqkb{Q+baUuh--8y@g`A7Xl)P>eg$C+Qmq$N0K1kf=} z6V96L?+Jjuw5SR_I6?%^xJR1Z z5`6D6QRkZax;9tsN@kl)tiCCd*epr&xo+ag@buqV!CWI=x3sB*A)UX zUozyuKlIDJ0&)9FlD9Q^qnO`#Azj3Z`Hkn(g{O7h%sRmQ1{aXkygv$nip0nPIS7&q zktB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P`a)3G2YeVZNop88g8FoC$ZitA1wi@c1VuN8f1CH<{o@4Dk>Y zBPy@vp_*LQR>TuL-_h3ab)H=9ZSMEX(f0ATJh{nX(V+HgQ4K!inWJsv)pK&S8C|!| z*`bM_2lfc_q1u0HYxs%lcf=PvCH8l|Zte=r;)mugWDU7IVcr5~vv`*P(Z@JC?N?_8 zB1`yP^Gcl;Go^aAobU?`NMT86E8?%rTfmC*_@#NJ?tJx5x9beRI3^rkh5gF99K3G+ zBiTRyy=fEaHJ)(jL|o7bo`i4Uy$zmsdBQiP~t(A=pKcy zQ9+xO*~3H;~zE-{(v`m(TBRgGcirQx}$?B z+vrItn$WLsU2S}!GWb+%SBvF0FHB!F$sv74U-Hp0!y&oN6I~3zZdm{xg?rUU0{fZ) zFwgeZsI-5}vW?InC#+ZayK2`yg>P2)D&_4hg+DC3HUE#&E>@h_zp4Q|t?(iB6(j2H zS%tSKR`q~j4`yJ5&ct^y`{6yI=Q-yC|!T>G_t(L)`G1GG@49Uxo z>YI0zv$+P~wu@;spS~#Fz19Gdk%@`wtfF6JHY@w0{I0teP1ChIc;n)$`LP>Xx^Aj3 z)4R4US)*xf{LIZ|$!pXnt?2wV&s$oSJVSMHbgtu#OUvT7DRdj(y|gl3{M)q$e{X5! z=*1AdAolURTYOp%f9965(GP9W%6Z@#{=qG!32lmiwj%CqSfJg`8ym`!zws4~Khsc_ zyzx32DmqW=I@2&m)4=nVm&IF(aEsr%d=87$@+X#i#PqM_o`h-FOT$Pkcdw|3FIptU Uhk4_Q%J>&=l+rakZ)xs-0M{h1YybcN diff --git a/final/editor.c b/final/editor.c index d1d5ea2..c5636c0 100644 --- a/final/editor.c +++ b/final/editor.c @@ -23,6 +23,8 @@ void add_toolbar(Editor* self) { addstr(mode); } +#define min(x, y) ((x) < (y) ? (x) : (y)) + void refresh_buffer(Editor* self) { move(0, 0); @@ -36,24 +38,62 @@ void refresh_buffer(Editor* self) { for (size_t i = self->y_offset; i < self->lines && i < self->y_offset + max_y; i++) { // for (size_t i = 0; i < self->lines; i++) { // adding the line number - char line_no[5]; + char line_no[6]; snprintf(line_no, 5, "%-5d", i + 1); // adding the line of text to the buffer addstr(line_no); addch(' '); - addstr(to_chars(&self->buffer[i])); + + String_t line_segment = str_slice( + &self->buffer[i], + self->x_offset, + min(str_len(&self->buffer[i]), self->x_offset + max_x - 5) + ); + + addstr(to_chars(&line_segment)); + str_dealloc(&line_segment); + addstr("\n"); } add_toolbar(self); - move(self->buffer_line, self->buffer_col + 5); + move(self->buffer_line - self->y_offset, self->buffer_col - self->x_offset + 5); } void move_cursor_on_screen(Editor* editor, int x, int y) { int newx = x + 5; // account for the line number - move(y, newx); + int max_x, max_y; + getmaxyx(stdscr, max_y, max_x); + + bool flag = false; + + while (newx + 3 > max_x + editor->x_offset) { + editor->x_offset += 1; + flag = true; + } + + while (newx - 3 < editor->x_offset) { + editor->x_offset -= 1; + flag = true; + } + + while (y + 3 > max_y + editor->y_offset) { + editor->y_offset += 1; + flag = true; + } + + while (y - 3 < editor->y_offset) { + editor->y_offset -= 1; + flag = true; + } + + if (flag) { + refresh_buffer(editor); + } + + move(y - editor->y_offset, newx - editor->x_offset); } void switch_mode(Editor* self) { @@ -63,12 +103,15 @@ void switch_mode(Editor* self) { void newline(Editor* self) { move(self->buffer_line, 0); - char line_no[5]; + char line_no[6]; snprintf(line_no, 5, "%-5d", self->buffer_line + 1); + + fprintf(stderr, "ln: %d, col: %d", self->buffer_line, self->buffer_col); + // adding the line of text to the buffer addstr(line_no); addch(' '); - move(self->buffer_line, self->buffer_col + 5); + move_cursor_on_screen(self, self->buffer_col, self->buffer_line); } Editor editor_from(String_t input_string) { @@ -120,6 +163,7 @@ void move_cursor(Editor* self, int x, int y) { // moving the cursor left or right in any other case self->buffer_col += x; } + fprintf(stderr, "ln: %d, col: %d xoff: %d yoff: %d\n", self->buffer_line, self->buffer_col, self->x_offset, self->y_offset); move_cursor_on_screen(self, self->buffer_col, self->buffer_line); } diff --git a/final/log.txt b/final/log.txt index 50edf5f..19e8c59 100644 --- a/final/log.txt +++ b/final/log.txt @@ -1 +1,258 @@ -Fatal glibc error: malloc.c:2599 (sysmalloc): assertion failed: (old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0) +starting editor openiing file somecode.cln: 1, col: 0 xoff: 0 yoff: 0 +ln: 2, col: 0 xoff: 0 yoff: 0 +ln: 3, col: 0 xoff: 0 yoff: 0 +ln: 4, col: 0 xoff: 0 yoff: 0 +ln: 5, col: 0 xoff: 0 yoff: 0 +ln: 6, col: 0 xoff: 0 yoff: 0 +ln: 7, col: 0 xoff: 0 yoff: 0 +ln: 8, col: 0 xoff: 0 yoff: 0 +ln: 9, col: 0 xoff: 0 yoff: 0 +ln: 10, col: 0 xoff: 0 yoff: 0 +ln: 11, col: 0 xoff: 0 yoff: 0 +ln: 12, col: 0 xoff: 0 yoff: 0 +ln: 13, col: 0 xoff: 0 yoff: 0 +ln: 14, col: 0 xoff: 0 yoff: 0 +ln: 15, col: 0 xoff: 0 yoff: 0 +ln: 16, col: 0 xoff: 0 yoff: 0 +ln: 17, col: 0 xoff: 0 yoff: 0 +ln: 18, col: 0 xoff: 0 yoff: 0 +ln: 19, col: 0 xoff: 0 yoff: 0 +ln: 20, col: 0 xoff: 0 yoff: 0 +ln: 21, col: 0 xoff: 0 yoff: 0 +ln: 22, col: 0 xoff: 0 yoff: 0 +ln: 23, col: 0 xoff: 0 yoff: 0 +ln: 24, col: 0 xoff: 0 yoff: 0 +ln: 25, col: 0 xoff: 0 yoff: 0 +ln: 26, col: 0 xoff: 0 yoff: 0 +ln: 27, col: 0 xoff: 0 yoff: 0 +ln: 28, col: 0 xoff: 0 yoff: 0 +ln: 29, col: 0 xoff: 0 yoff: 0 +ln: 30, col: 0 xoff: 0 yoff: 0 +ln: 31, col: 0 xoff: 0 yoff: 0 +ln: 32, col: 0 xoff: 0 yoff: 0 +ln: 33, col: 0 xoff: 0 yoff: 0 +ln: 34, col: 0 xoff: 0 yoff: 0 +ln: 35, col: 0 xoff: 0 yoff: 0 +ln: 36, col: 0 xoff: 0 yoff: 0 +ln: 37, col: 0 xoff: 0 yoff: 1 +ln: 38, col: 0 xoff: 0 yoff: 2 +ln: 39, col: 0 xoff: 0 yoff: 3 +ln: 40, col: 0 xoff: 0 yoff: 4 +ln: 41, col: 0 xoff: 0 yoff: 5 +ln: 42, col: 0 xoff: 0 yoff: 6 +ln: 43, col: 0 xoff: 0 yoff: 7 +ln: 44, col: 0 xoff: 0 yoff: 8 +ln: 45, col: 0 xoff: 0 yoff: 9 +ln: 46, col: 0 xoff: 0 yoff: 10 +ln: 47, col: 0 xoff: 0 yoff: 11 +ln: 48, col: 0 xoff: 0 yoff: 12 +ln: 49, col: 0 xoff: 0 yoff: 13 +ln: 50, col: 0 xoff: 0 yoff: 14 +ln: 51, col: 0 xoff: 0 yoff: 15 +ln: 52, col: 0 xoff: 0 yoff: 16 +ln: 53, col: 0 xoff: 0 yoff: 17 +ln: 54, col: 0 xoff: 0 yoff: 18 +ln: 55, col: 0 xoff: 0 yoff: 19 +ln: 56, col: 0 xoff: 0 yoff: 20 +ln: 57, col: 0 xoff: 0 yoff: 21 +ln: 58, col: 0 xoff: 0 yoff: 22 +ln: 59, col: 0 xoff: 0 yoff: 23 +ln: 60, col: 0 xoff: 0 yoff: 24 +ln: 61, col: 0 xoff: 0 yoff: 25 +ln: 62, col: 0 xoff: 0 yoff: 26 +ln: 63, col: 0 xoff: 0 yoff: 27 +ln: 64, col: 0 xoff: 0 yoff: 28 +ln: 65, col: 0 xoff: 0 yoff: 29 +ln: 66, col: 0 xoff: 0 yoff: 30 +ln: 67, col: 0 xoff: 0 yoff: 31 +ln: 68, col: 0 xoff: 0 yoff: 32 +ln: 69, col: 0 xoff: 0 yoff: 33 +ln: 70, col: 0 xoff: 0 yoff: 34 +ln: 71, col: 0 xoff: 0 yoff: 35 +ln: 72, col: 0 xoff: 0 yoff: 36 +ln: 73, col: 0 xoff: 0 yoff: 37 +ln: 74, col: 0 xoff: 0 yoff: 38 +ln: 75, col: 0 xoff: 0 yoff: 39 +ln: 76, col: 0 xoff: 0 yoff: 40 +ln: 77, col: 0 xoff: 0 yoff: 41 +ln: 78, col: 0 xoff: 0 yoff: 42 +ln: 79, col: 0 xoff: 0 yoff: 43 +ln: 80, col: 0 xoff: 0 yoff: 44 +ln: 81, col: 0 xoff: 0 yoff: 45 +ln: 82, col: 0 xoff: 0 yoff: 46 +ln: 83, col: 0 xoff: 0 yoff: 47 +ln: 84, col: 0 xoff: 0 yoff: 48 +ln: 85, col: 0 xoff: 0 yoff: 49 +ln: 86, col: 0 xoff: 0 yoff: 50 +ln: 87, col: 0 xoff: 0 yoff: 51 +ln: 88, col: 0 xoff: 0 yoff: 52 +ln: 89, col: 0 xoff: 0 yoff: 53 +ln: 90, col: 0 xoff: 0 yoff: 54 +ln: 91, col: 0 xoff: 0 yoff: 55 +ln: 92, col: 0 xoff: 0 yoff: 56 +ln: 93, col: 0 xoff: 0 yoff: 57 +ln: 94, col: 0 xoff: 0 yoff: 58 +ln: 95, col: 0 xoff: 0 yoff: 59 +ln: 96, col: 0 xoff: 0 yoff: 60 +ln: 97, col: 0 xoff: 0 yoff: 61 +ln: 98, col: 0 xoff: 0 yoff: 62 +ln: 99, col: 0 xoff: 0 yoff: 63 +ln: 100, col: 0 xoff: 0 yoff: 64 +ln: 101, col: 0 xoff: 0 yoff: 65 +ln: 102, col: 0 xoff: 0 yoff: 66 +ln: 103, col: 0 xoff: 0 yoff: 67 +ln: 104, col: 0 xoff: 0 yoff: 68 +ln: 105, col: 0 xoff: 0 yoff: 69 +ln: 106, col: 0 xoff: 0 yoff: 70 +ln: 107, col: 0 xoff: 0 yoff: 71 +ln: 108, col: 0 xoff: 0 yoff: 72 +ln: 109, col: 0 xoff: 0 yoff: 73 +ln: 110, col: 0 xoff: 0 yoff: 74 +ln: 111, col: 0 xoff: 0 yoff: 75 +ln: 112, col: 0 xoff: 0 yoff: 76 +ln: 113, col: 0 xoff: 0 yoff: 77 +ln: 114, col: 0 xoff: 0 yoff: 78 +ln: 115, col: 0 xoff: 0 yoff: 79 +ln: 116, col: 0 xoff: 0 yoff: 80 +ln: 117, col: 0 xoff: 0 yoff: 81 +ln: 118, col: 0 xoff: 0 yoff: 82 +ln: 119, col: 0 xoff: 0 yoff: 83 +ln: 120, col: 0 xoff: 0 yoff: 84 +ln: 121, col: 0 xoff: 0 yoff: 85 +ln: 122, col: 0 xoff: 0 yoff: 86 +ln: 123, col: 0 xoff: 0 yoff: 87 +ln: 124, col: 0 xoff: 0 yoff: 88 +ln: 125, col: 0 xoff: 0 yoff: 89 +ln: 126, col: 0 xoff: 0 yoff: 90 +ln: 127, col: 0 xoff: 0 yoff: 91 +ln: 128, col: 0 xoff: 0 yoff: 92 +ln: 129, col: 0 xoff: 0 yoff: 93 +ln: 130, col: 0 xoff: 0 yoff: 94 +ln: 131, col: 0 xoff: 0 yoff: 95 +ln: 132, col: 0 xoff: 0 yoff: 96 +ln: 133, col: 0 xoff: 0 yoff: 97 +ln: 134, col: 0 xoff: 0 yoff: 98 +ln: 135, col: 0 xoff: 0 yoff: 99 +ln: 136, col: 0 xoff: 0 yoff: 100 +ln: 137, col: 0 xoff: 0 yoff: 101 +ln: 138, col: 0 xoff: 0 yoff: 102 +ln: 139, col: 0 xoff: 0 yoff: 103 +ln: 140, col: 0 xoff: 0 yoff: 104 +ln: 141, col: 0 xoff: 0 yoff: 105 +ln: 142, col: 0 xoff: 0 yoff: 106 +ln: 143, col: 0 xoff: 0 yoff: 107 +ln: 144, col: 0 xoff: 0 yoff: 108 +ln: 145, col: 0 xoff: 0 yoff: 109 +ln: 146, col: 0 xoff: 0 yoff: 110 +ln: 147, col: 0 xoff: 0 yoff: 111 +ln: 148, col: 0 xoff: 0 yoff: 112 +ln: 149, col: 0 xoff: 0 yoff: 113 +ln: 150, col: 0 xoff: 0 yoff: 114 +ln: 151, col: 0 xoff: 0 yoff: 115 +ln: 152, col: 0 xoff: 0 yoff: 116 +ln: 153, col: 0 xoff: 0 yoff: 117 +ln: 154, col: 0 xoff: 0 yoff: 118 +ln: 155, col: 0 xoff: 0 yoff: 119 +ln: 156, col: 0 xoff: 0 yoff: 120 +ln: 157, col: 0 xoff: 0 yoff: 121 +ln: 158, col: 0 xoff: 0 yoff: 122 +ln: 159, col: 0 xoff: 0 yoff: 123 +ln: 160, col: 0 xoff: 0 yoff: 124 +ln: 161, col: 0 xoff: 0 yoff: 125 +ln: 162, col: 0 xoff: 0 yoff: 126 +ln: 163, col: 0 xoff: 0 yoff: 127 +ln: 164, col: 0 xoff: 0 yoff: 128 +ln: 165, col: 0 xoff: 0 yoff: 129 +ln: 166, col: 0 xoff: 0 yoff: 130 +ln: 167, col: 0 xoff: 0 yoff: 131 +ln: 168, col: 0 xoff: 0 yoff: 132 +ln: 169, col: 0 xoff: 0 yoff: 133 +ln: 170, col: 0 xoff: 0 yoff: 134 +ln: 171, col: 0 xoff: 0 yoff: 135 +ln: 172, col: 0 xoff: 0 yoff: 136 +ln: 173, col: 0 xoff: 0 yoff: 137 +ln: 174, col: 0 xoff: 0 yoff: 138 +ln: 175, col: 0 xoff: 0 yoff: 139 +ln: 176, col: 0 xoff: 0 yoff: 140 +ln: 177, col: 0 xoff: 0 yoff: 141 +ln: 178, col: 0 xoff: 0 yoff: 142 +ln: 178, col: 0 xoff: 0 yoff: 143 +ln: 178, col: 0 xoff: 0 yoff: 143 +ln: 178, col: 0 xoff: 0 yoff: 143 +ln: 178, col: 0 xoff: 0 yoff: 143 +ln: 178, col: 0 xoff: 0 yoff: 143 +ln: 178, col: 0 xoff: 0 yoff: 143 +ln: 178, col: 0 xoff: 0 yoff: 143 +ln: 178, col: 0 xoff: 0 yoff: 143 +ln: 178, col: 0 xoff: 0 yoff: 143 +ln: 178, col: 0 xoff: 0 yoff: 143 +ln: 178, col: 0 xoff: 0 yoff: 143 +ln: 178, col: 0 xoff: 0 yoff: 143 +ln: 178, col: 0 xoff: 0 yoff: 143 +ln: 178, col: 0 xoff: 0 yoff: 143 +ln: 178, col: 0 xoff: 0 yoff: 143 +ln: 178, col: 0 xoff: 0 yoff: 143 +105 +ln: 178, col: 0ln: 178, col: 1 xoff: 0 yoff: 143 +100 +ln: 178, col: 2 xoff: 0 yoff: 143 +97 +ln: 178, col: 3 xoff: 0 yoff: 143 +109 +ln: 178, col: 4 xoff: 0 yoff: 143 +110 +ln: 178, col: 5 xoff: 0 yoff: 143 +32 +ln: 178, col: 6 xoff: 0 yoff: 143 +ln: 178, col: 5 xoff: 0 yoff: 143 +ln: 178, col: 4 xoff: 0 yoff: 143 +ln: 178, col: 3 xoff: 0 yoff: 143 +ln: 178, col: 2 xoff: 0 yoff: 143 +ln: 178, col: 1 xoff: 0 yoff: 143 +ln: 178, col: 0 xoff: 0 yoff: 143 +ln: 177, col: 4 xoff: 0 yoff: 143 +ln: 178, col: 0 xoff: 0 yoff: 143 +105 +ln: 178, col: 1 xoff: 0 yoff: 143 +ln: 178, col: 0 xoff: 0 yoff: 143 +ln: 177, col: 4 xoff: 0 yoff: 143 +ln: 178, col: 0 xoff: 0 yoff: 143 +ln: 177, col: 4 xoff: 0 yoff: 143 +ln: 178, col: 0 xoff: 0 yoff: 143 +ln: 177, col: 4 xoff: 0 yoff: 143 +ln: 178, col: 0 xoff: 0 yoff: 143 +ln: 177, col: 0 xoff: 0 yoff: 143 +ln: 178, col: 0 xoff: 0 yoff: 143 +ln: 177, col: 4 xoff: 0 yoff: 143 +ln: 178, col: 0 xoff: 0 yoff: 143 +ln: 177, col: 4 xoff: 0 yoff: 143 +ln: 178, col: 0 xoff: 0 yoff: 143 +ln: 177, col: 4 xoff: 0 yoff: 143 +ln: 178, col: 0 xoff: 0 yoff: 143 +ln: 178, col: 0 xoff: 0 yoff: 143 +ln: 178, col: 0 xoff: 0 yoff: 143 +ln: 178, col: 0 xoff: 0 yoff: 143 +111 +ln: 178, col: 0ln: 178, col: 1 xoff: 0 yoff: 143 +ln: 177, col: 1 xoff: 0 yoff: 143 +ln: 176, col: 1 xoff: 0 yoff: 143 +ln: 175, col: 1 xoff: 0 yoff: 143 +410 +ln: 175, col: 2 xoff: 0 yoff: 143 +410 +ln: 175, col: 3 xoff: 0 yoff: 155 +ln: 175, col: 4 xoff: 0 yoff: 155 +ln: 175, col: 3 xoff: 0 yoff: 155 +ln: 175, col: 2 xoff: 0 yoff: 155 +ln: 175, col: 1 xoff: 0 yoff: 155 +ln: 175, col: 2 xoff: 0 yoff: 155 +ln: 174, col: 2 xoff: 0 yoff: 155 +ln: 175, col: 2 xoff: 0 yoff: 155 +ln: 176, col: 2 xoff: 0 yoff: 155 +ln: 176, col: 1 xoff: 0 yoff: 155 +ln: 176, col: 0 xoff: 0 yoff: 155 +ln: 175, col: 22 xoff: 0 yoff: 155 +ln: 176, col: 0 xoff: 0 yoff: 155 +ln: 176, col: 1 xoff: 0 yoff: 155 +ln: 176, col: 0 xoff: 0 yoff: 155 +ln: 175, col: 22 xoff: 0 yoff: 155 diff --git a/final/somefile.c b/final/somefile.c deleted file mode 100644 index 745908c..0000000 --- a/final/somefile.c +++ /dev/null @@ -1,221 +0,0 @@ -/// dynamic array class -/// written by: Harry Irving - -#include -#include -#include -#include -#include - -typedef struct { - uint32_t size, capacity; - char* data; -} String_t; - -String_t str_with_capacity(int capacity) { - String_t s; - - /// allocate memory for 'capacity' chars - s.data = (char*)calloc(capacity, sizeof(char)); - - s.size = 0; - s.capacity = capacity; - - return s; -} - -String_t str_from_chars(const char* string) { - String_t s; - - s.data = (char*)calloc(strlen(string), sizeof(char)); - strcpy(s.data, string); - s.size = strlen(string); - s.capacity = strlen(string); - return s; -} - -String_t str_from_slice(const char* string, int len) { - String_t s; - - s.data = (char*)calloc(len, sizeof(char)); - strncpy(s.data, string, len); - s.size = len; - s.capacity = len; - return s; -} - - -String_t str_new() { - return str_with_capacity(1); -} - - - -int str_dealloc(String_t* self) { - free(self->data); - return 0; -} - -int str_push(String_t* self, char c) { - // check size < capacity - if (self->size < self->capacity) { - self->data[self->size] = c; - self->size++; - return 0; - } - - // reallocate to add capacity for an extra char - int newcap = self->capacity + sizeof(char); - self->data = (char*)realloc(self->data, newcap); - self->data[self->size] = c; - self->size++; - self->capacity = newcap; - return 0; -} - -int str_push_str(String_t* self, char* c) { - if (self->size + strlen(c) >= self->capacity) { - self->capacity = ( self->size + strlen(c) ) * sizeof(char); - self->data = (char*)realloc(self->data, self->capacity); - - for (int i = 0; i < strlen(c); i++) { - self->data[self->size + i] = c[i]; - } - self->size = self->capacity; - } -} - -String_t str_from_file(FILE* file) { - if (file == NULL) { - return str_new(); - } - - char buffer[1024]; - - String_t string = str_new(); - - while (fgets(buffer, sizeof(buffer), file) != NULL) { - str_push_str(&string, buffer); - } - - fclose(file); - - return string; -} - -char str_pop(String_t* self) { - if (self->size == 0) { - return '\0'; - } - self->size--; - char c = self->data[self->size]; - return c; -} - -int str_insert(String_t* self, int index, char c) { - if (index > self->size) { - return -1; - } - self->size++; - for (int i = self->size - 1; i > index; i--) { - self->data[i] = self->data[i - 1]; - } - self->data[index] = c; - return 0; -} - -int str_remove(String_t* self, int index) { - if (index >= self->size) { - return -1; - } - self->size--; - for (int i = index; i < self->size; i++) { - self->data[i] = self->data[i + 1]; - } - return 0; -} - -/// splits a string into an array of strings based on a delimiter -String_t* str_split(String_t* self, int* res_len, char c) { - - char* string = self->data; - String_t* elements = NULL; - - // find the number of lines in the file - - bool flag = false; - *res_len = 0; - for (int i = 0; i < strlen(string); i++) { - if (string[i] == c) { - if (flag) { - (*res_len)++; - flag = false; - } - } else { - flag = true; - } - } - if (flag) { - (*res_len)++; - } - - // allocate memory for an array of pointers to each line - elements = (String_t*)malloc((*res_len) * sizeof(String_t)); - - int i = 0; - flag = false; - char* start = string; - char* end = string; - - while (*end != '\0') { - if (*end == c) { - if (flag) { - elements[i] = str_from_slice(start, end - start); - i++; - } - end++; - start = end; - flag = false; - } else { - end++; - flag = true; - } - } if (flag) { - elements[i] = str_from_slice(start, end - start); - } - - - // returns an array of String_t - return elements; -} - -String_t* str_lines(String_t* self, int* numlines) { - return str_split(self, numlines, '\n'); -} - -int str_len(String_t* s) { - return s->size; -} - -char* to_chars(String_t* s) { - return s->data; -} - -// int main() { -// String_t s = str_from_chars("hello\nworld\neeeee\notherline\n"); - -// str_remove(&s, 10); -// str_insert(&s, 10, 'h'); - -// printf("%s\n", to_chars(&s)); - -// int linen = 0; -// String_t* lines = str_lines(&s, &linen); -// for (int i=0; i < linen; i++) { -// printf("%s", to_chars(&lines[i])); -// } - -// str_dealloc(&s); - -// return 0; -// }