aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authornytpu <alex@nytpu.com>2021-06-04 16:09:11 -0600
committernytpu <alex@nytpu.com>2021-06-04 16:09:11 -0600
commit7aff99458d98dd75ef6ee0d56dbde9ce3c177ae5 (patch)
tree7f4815bfb18d7febc80670ab3f9a16c92944e299
parentadd framework for commands (diff)
downloaded-7aff99458d98dd75ef6ee0d56dbde9ce3c177ae5.tar.bz2
ed-7aff99458d98dd75ef6ee0d56dbde9ce3c177ae5.zip
use one-indexing interally unless actually indexing rows array
-rw-r--r--buffer.c20
-rw-r--r--buffer.h6
-rw-r--r--common.h2
-rw-r--r--io.c36
-rw-r--r--io.h3
-rw-r--r--main.c3
-rw-r--r--test.txt10
7 files changed, 56 insertions, 24 deletions
diff --git a/buffer.c b/buffer.c
index 7005651..b863a16 100644
--- a/buffer.c
+++ b/buffer.c
@@ -10,10 +10,19 @@
#include <stdlib.h>
#include <string.h>
+bool
+in_bounds(RowNum at, RowNum top)
+{
+ if (at < 1 || at > top) return false;
+ return true;
+}
+
void
insert_row(RowNum at, char *r, size_t len)
{
- if (at > E.numrows) return;
+ if (!in_bounds(at, E.numrows+1)) return;
+ --at;
+
E.rows = realloc(E.rows, (E.numrows+1) * sizeof(struct Row));
if (at != E.numrows) {
memmove(&E.rows[at+1], &E.rows[at], (E.numrows-at) * sizeof(struct Row));
@@ -31,7 +40,9 @@ insert_row(RowNum at, char *r, size_t len)
void
delete_row(RowNum at)
{
- if (at >= E.numrows) return;
+ if (!in_bounds(at, E.numrows)) return;
+ --at;
+
free(E.rows[at].chars);
memmove(&E.rows[at], &E.rows[at+1], (E.numrows-at-1) * sizeof(struct Row));
for (RowNum i = at; i < E.numrows; ++i) --E.rows[i].idx;
@@ -42,6 +53,7 @@ delete_row(RowNum at)
void
delete_rows(RowNum from, RowNum to)
{
- if (from < 0 || to >= E.numrows) return;
- for (; from != to; ++from) delete_row(from);
+ if (!in_bounds(from, E.numrows)) return;
+ if (!in_bounds(to, E.numrows)) return;
+ for (; from <= to; ++from) delete_row(from);
}
diff --git a/buffer.h b/buffer.h
index ec9ff1c..f85b31f 100644
--- a/buffer.h
+++ b/buffer.h
@@ -11,11 +11,15 @@
#include <stdlib.h>
+// check if at is in range [1, top]
+bool in_bounds(RowNum at, RowNum top);
+
// insert a row at the specified position, moving other rows if necessary
void insert_row(RowNum at, char *r, size_t len);
+
// delete a row at the specified position, moving other rows if necessary
void delete_row(RowNum at);
-// delete rows in range [from, to)
+// delete rows in range [from, to]
void delete_rows(RowNum from, RowNum to);
#endif // _BUFFER_H
diff --git a/common.h b/common.h
index e041128..7d7b165 100644
--- a/common.h
+++ b/common.h
@@ -50,7 +50,7 @@ struct Editor {
/* misc state information */
bool dirty; // has the buffer been modified since last write?
int scols; // number of columns we can display on screen
- RowNum crow; // current row address
+ RowNum crow; // current row address - ONE INDEXED
const char *last_err; // verbose error message for previous error
};
diff --git a/io.c b/io.c
index 4ccca66..d14a65d 100644
--- a/io.c
+++ b/io.c
@@ -35,7 +35,7 @@ rows_to_string(long *buflen)
return buf;
}
-int
+void
open_file(const char *fn, bool force)
{
if (E.dirty && !force) {
@@ -52,7 +52,7 @@ open_file(const char *fn, bool force)
if (errno != ENOENT) {
die("Opening file");
}
- return -1;
+ return;
}
char *l = NULL;
@@ -61,7 +61,7 @@ open_file(const char *fn, bool force)
long tot = 0;
while ((llen = getline(&l, &lcap, f)) != -1) {
while (llen > 0 && (l[llen-1] == '\n')) --llen;
- insert_row(E.numrows, l, llen);
+ insert_row(E.numrows+1, l, llen);
tot += llen + 1;
}
free(l);
@@ -69,10 +69,9 @@ open_file(const char *fn, bool force)
E.crow = E.numrows;
E.dirty = false;
if (!E.quiet) printf("%ld\n", tot);
- return 0;
+ return;
}
-
int
write_file(const char *fn)
{
@@ -114,22 +113,26 @@ ioerr:
int
print_rows(RowNum from, RowNum to)
{
- if (from < 0 || to > E.numrows) return -1;
- for (; from != to; ++from) {
+ if (!in_bounds(from, E.numrows)) return -1;
+ if (!in_bounds(to, E.numrows)) return -1;
+ --from; --to;
+ for (; from <= to; ++from) {
printf("%s\n", E.rows[from].chars);
}
- E.crow = from;
+ E.crow = from + 1;
return 0;
}
int
print_rows_numbered(RowNum from, RowNum to)
{
- if (from < 0 || to > E.numrows) return -1;
- for (; from != to; ++from) {
- printf("%ld\t%s\n", from, E.rows[from].chars);
+ if (!in_bounds(from, E.numrows)) return -1;
+ if (!in_bounds(to, E.numrows)) return -1;
+ --from; --to;
+ for (; from <= to; ++from) {
+ printf("%ld\t%s\n", from+1, E.rows[from].chars);
}
- E.crow = from;
+ E.crow = from + 1;
return 0;
}
@@ -141,17 +144,18 @@ print_rows_escaped(RowNum from, RowNum to)
}
void
-insert_mode(RowNum target)
+insert_mode(RowNum at)
{
+ if (!in_bounds(at, E.numrows)) return;
char *l = NULL;
size_t lcap = 0;
ssize_t llen;
while ((llen = getline(&l, &lcap, stdin)) != -1) {
while (llen > 0 && (l[llen-1] == '\n')) --llen;
if (llen == 1 && l[0] == '.') break;
- insert_row(target, l, llen);
- ++target;
+ insert_row(at, l, llen);
+ ++at;
}
- E.crow = target;
+ E.crow = at;
free(l);
}
diff --git a/io.h b/io.h
index 322a9fc..0a1b5e5 100644
--- a/io.h
+++ b/io.h
@@ -11,8 +11,7 @@
#include <stdbool.h>
// load the specified file into buffer. will re-initialize buffer
-// Return: 0 on success, -1 on error
-int open_file(const char *filename, bool force);
+void open_file(const char *filename, bool force);
// write buffer to given filename, or use saved filename if argument is NULL
// Return: 0 on success, -1 on error
diff --git a/main.c b/main.c
index 1c97f9c..7b224be 100644
--- a/main.c
+++ b/main.c
@@ -88,6 +88,9 @@ main(int argc, char *argv[])
open_file(argv[optind], true);
}
+ // DEBUG
+ printf("#: %ld; c: %ld\n", E.numrows, E.crow);
+
char *line;
while ((line = linenoise(E.prompt_enabled ? E.prompt : "")) != NULL) {
struct command cmd;
diff --git a/test.txt b/test.txt
new file mode 100644
index 0000000..f00c965
--- /dev/null
+++ b/test.txt
@@ -0,0 +1,10 @@
+1
+2
+3
+4
+5
+6
+7
+8
+9
+10