moving fwd, very slowly
authorJohn Tsiombikas <nuclear@member.fsf.org>
Fri, 22 Nov 2019 01:00:31 +0000 (03:00 +0200)
committerJohn Tsiombikas <nuclear@member.fsf.org>
Fri, 22 Nov 2019 01:00:31 +0000 (03:00 +0200)
libvisor/include/visor.h
libvisor/src/visor.c
visor/Makefile
visor/src/main_unix.c

index 1e79aed..58b6ac4 100644 (file)
@@ -77,8 +77,8 @@ enum { VI_SEEK_SET, VI_SEEK_CUR, VI_SEEK_END };
 
 struct vi_fileops {
        vi_file *(*open)(const char *path, unsigned int flags);
 
 struct vi_fileops {
        vi_file *(*open)(const char *path, unsigned int flags);
-       long (*size)(vi_file *file);
        void (*close)(vi_file *file);
        void (*close)(vi_file *file);
+       long (*size)(vi_file *file);
        void *(*map)(vi_file *file);
        void (*unmap)(vi_file *file);
        long (*read)(vi_file *file, void *buf, long count);
        void *(*map)(vi_file *file);
        void (*unmap)(vi_file *file);
        long (*read)(vi_file *file, void *buf, long count);
index 1f9804f..9489680 100644 (file)
@@ -213,7 +213,7 @@ void split_span(struct vi_buffer *vb, struct vi_span *sp, vi_addr spoffs, unsign
 
        if(tail > sp) {
                /* we produced one or more zero-sized spans, drop them */
 
        if(tail > sp) {
                /* we produced one or more zero-sized spans, drop them */
-               num_move = vb->spans + vb->num_spans - (tail - sp);
+               num_move = vb->num_spans - (tail - sp);
                memmove(sp, tail, num_move * sizeof *sp);
                vb->num_spans -= num_move;
        }
                memmove(sp, tail, num_move * sizeof *sp);
                vb->num_spans -= num_move;
        }
@@ -223,6 +223,7 @@ static int add_span(struct vi_buffer *vb, vi_addr at, int src, vi_addr start, un
 {
        struct visor *vi = vb->vi;
        struct vi_span *sp;
 {
        struct visor *vi = vb->vi;
        struct vi_span *sp;
+       vi_addr spoffs;
 
        /* make sure we have space for at least two new spans (split + add) */
        if(vb->num_spans + 1 >= vb->max_spans) {
 
        /* make sure we have space for at least two new spans (split + add) */
        if(vb->num_spans + 1 >= vb->max_spans) {
@@ -371,14 +372,16 @@ long vi_buf_size(struct vi_buffer *vb)
        return sz;
 }
 
        return sz;
 }
 
-struct vi_span *vi_buf_find_span(struct vi_buffer *vb, vi_addr at)
+struct vi_span *vi_buf_find_span(struct vi_buffer *vb, vi_addr at, vi_addr *soffs)
 {
        int i;
 {
        int i;
-       long sz = 0;
+       long sz = 0, prev_sz;
 
        for(i=0; i<vb->num_spans; i++) {
 
        for(i=0; i<vb->num_spans; i++) {
+               prev_sz = sz;
                sz += vb->spans[i].size;
                if(sz > at) {
                sz += vb->spans[i].size;
                if(sz > at) {
+                       if(soffs) *soffs = at - prev_sz;
                        return vb->spans + i;
                }
        }
                        return vb->spans + i;
                }
        }
index b97b548..dd9e7b1 100644 (file)
@@ -3,7 +3,8 @@ obj = $(src:.c=.o)
 dep = $(obj:.o=.d)
 bin = visor
 
 dep = $(obj:.o=.d)
 bin = visor
 
-CFLAGS = -pedantic -Wall -g
+CFLAGS = -pedantic -Wall -g -I../libvisor/include
+LDFLAGS = -L../libvisor -lvisor
 
 $(bin): $(obj)
        $(CC) -o $@ $(obj) $(LDFLAGS)
 
 $(bin): $(obj)
        $(CC) -o $@ $(obj) $(LDFLAGS)
index 9db4e36..6430272 100644 (file)
@@ -1,13 +1,50 @@
 #include <stdio.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include "term.h"
 #include "term.h"
+#include "visor.h"
+
+struct file {
+       FILE *fp;
+       void *maddr;
+       size_t msize;
+};
 
 static int parse_args(int argc, char **argv);
 static int init(void);
 static void cleanup(void);
 
 static int parse_args(int argc, char **argv);
 static int init(void);
 static void cleanup(void);
+/* file operations */
+static vi_file *file_open(const char *path, unsigned int flags);
+static void file_close(vi_file *file);
+static long file_size(vi_file *file);
+static void *file_map(vi_file *file);
+static void file_unmap(vi_file *file);
+static long file_read(vi_file *file, void *buf, long count);
+static long file_write(vi_file *file, void *buf, long count);
+static long file_seek(vi_file *file, long offs, int whence);
+
+static struct visor *vi;
 
 static int num_fpaths;
 static char **fpaths;
 
 
 static int num_fpaths;
 static char **fpaths;
 
+static struct vi_alloc alloc = {
+       malloc, free, realloc
+};
+
+static struct vi_fileops fops = {
+       file_open, file_close, file_size,
+       file_map, file_unmap,
+       file_read, file_write, file_seek
+};
+
+/*
+static struct vi_ttyops ttyops = {
+       tty_clear, tty_clear_line, tty_clear_line_at,
+       tty_setcursor, tty_putchar, tty_putchar_at,
+       tty_scroll, tty_del_back, tty_del_fwd, tty_status
+};
+*/
+
 int main(int argc, char **argv)
 {
        if(parse_args(argc, argv) == -1) {
 int main(int argc, char **argv)
 {
        if(parse_args(argc, argv) == -1) {
@@ -58,6 +95,11 @@ static int init(void)
        }
        term_clear();
 
        }
        term_clear();
 
+       if(!(vi = vi_create(&alloc))) {
+               return -1;
+       }
+       vi_set_fileops(vi, &fops);
+
        for(i=0; i<num_fpaths; i++) {
                /* open fpaths[i] */
        }
        for(i=0; i<num_fpaths; i++) {
                /* open fpaths[i] */
        }
@@ -66,5 +108,74 @@ static int init(void)
 
 static void cleanup(void)
 {
 
 static void cleanup(void)
 {
+       if(vi) {
+               vi_destroy(vi);
+       }
        term_cleanup();
 }
        term_cleanup();
 }
+
+static vi_file *file_open(const char *path, unsigned int flags)
+{
+       struct file *file;
+       const char *attr;
+
+       switch(flags & 0xff) {
+       case VI_RDONLY:
+               attr = "rb";
+               break;
+
+       case VI_WRONLY:
+               attr = (flags & VI_CREAT) ? "w+b" : "wb";
+               break;
+
+       case VI_RDWR:
+               attr = (flags & VI_CREAT) ? "w+b" : "r+b";
+               break;
+
+       default:
+               return 0;
+       }
+
+
+       if(!(file = calloc(1, sizeof *file))) {
+               return 0;
+       }
+       if(!(file->fp = fopen(path, attr))) {
+               free(file);
+               return 0;
+       }
+       return file;
+}
+
+static void file_close(vi_file *file)
+{
+}
+
+static long file_size(vi_file *file)
+{
+       return -1;
+}
+
+static void *file_map(vi_file *file)
+{
+       return 0;
+}
+
+static void file_unmap(vi_file *file)
+{
+}
+
+static long file_read(vi_file *file, void *buf, long count)
+{
+       return -1;
+}
+
+static long file_write(vi_file *file, void *buf, long count)
+{
+       return -1;
+}
+
+static long file_seek(vi_file *file, long offs, int whence)
+{
+       return -1;
+}