commit ed38381d34f25c4cec990f6a50179fc429b7e06e
parent e29c8c2ab9bdb177b8cb47a20810373bf6a5ea0a
Author: ssnf <ssnf@ssnf.xyz>
Date: Sun, 15 Aug 2021 16:22:47 +0000
moved string.c and file.c to sim.c, and cleaned header file
Diffstat:
D | file.c | | | 48 | ------------------------------------------------ |
M | sim.c | | | 181 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- |
M | sim.h | | | 41 | ----------------------------------------- |
D | string.c | | | 87 | ------------------------------------------------------------------------------- |
4 files changed, 179 insertions(+), 178 deletions(-)
diff --git a/file.c b/file.c
@@ -1,48 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-
-#include "sim.h"
-
-void
-file_close(File* f)
-{
- str_close(f->s);
- str_close(f->name);
- free(f->s);
- free(f->name);
-}
-
-void
-file_init(File* f)
-{
- f->s = emalloc(sizeof(String));
- f->name = emalloc(sizeof(String));
- str_init(f->s);
- str_init(f->name);
- f->dot.p0 = f->dot.p1 = 0;
-}
-
-void
-file_load(File* f)
-{
- FILE* disk;
-
- if (!(disk = fopen(f->name->s, "r")))
- return;
- fseek(disk, 0, SEEK_END);
- f->s->n = ftell(disk);
- str_insure(f->s, f->s->n);
- rewind(disk);
- fread(f->s->s, f->s->n, 1, disk);
- fclose(disk);
-}
-
-void
-file_save(File* f)
-{
- FILE* disk;
-
- disk = fopen(f->name->s, "w");
- fwrite(f->s->s, f->s->n, 1, disk);
- fclose(disk);
-}
diff --git a/sim.c b/sim.c
@@ -5,6 +5,42 @@
#include "sim.h"
+#define MINSIZE 16
+#define MAXEMPTY 256
+#define FILECOUNT 8
+#define LENGTH(a) (sizeof(a)/sizeof(a[0]))
+
+typedef long Posn;
+
+enum {
+ Up,
+ Down,
+ Left,
+ Right
+};
+
+typedef struct {
+ Posn p0, p1;
+} Address ;
+
+typedef struct {
+ char* s;
+ ulong n; /*number of filled characters*/
+ ulong size;
+} String;
+
+typedef struct {
+ String* s;
+ String* name;
+ Address dot;
+} File;
+
+typedef struct {
+ uchar key;
+ void (*func)(int);
+ int value;
+} Key;
+
typedef struct {
Address* a;
ushort dot;
@@ -18,6 +54,10 @@ void* erealloc(void*, ulong);
static void blind_reader(Frame* fr, Posn p0);
static void blind_writer(ushort line, ushort offset, ushort top, ushort bot);
static void init();
+static void file_close(File* f);
+static void file_init(File* f);
+static void file_load(File* f);
+static void file_save(File* f);
static void fr_add(Frame* fr, Posn p0, Posn p1);
static void fr_close(Frame* fr);
static void fr_init(Frame* fr);
@@ -25,6 +65,15 @@ static void fr_insert(Frame* p, Frame* q, ushort n);
static void fr_insure(Frame* fr, ushort n);
static void fr_update();
static void fr_zero(Frame*);
+static void str_init(String* p);
+static void str_close(String* p);
+static void str_zero(String* p);
+static void str_insure(String* p, ulong n);
+static void str_addc(String* p, int c);
+static void str_adds(String* p, char* s);
+static void str_delc(String* p);
+static void str_insert(String* p, String* q, Posn p0);
+static void str_delete(String* p, Posn p0, Posn p1);
static void move(int);
static void quit(int);
@@ -155,6 +204,8 @@ blind_writer(ushort line, ushort offset, ushort top, ushort bot)
static void
init()
{
+ uint i;
+
win_init();
fr_init(&frame);
for (i = 0; i < FILECOUNT; ++i)
@@ -163,6 +214,52 @@ init()
}
static void
+file_close(File* f)
+{
+ str_zero(f->s);
+ str_zero(f->name);
+ str_close(f->s);
+ str_close(f->name);
+ free(f->s);
+ free(f->name);
+}
+
+static void
+file_init(File* f)
+{
+ f->s = emalloc(sizeof(String));
+ f->name = emalloc(sizeof(String));
+ str_init(f->s);
+ str_init(f->name);
+ f->dot.p0 = f->dot.p1 = 0;
+}
+
+static void
+file_load(File* f)
+{
+ FILE* disk;
+
+ if (!(disk = fopen(f->name->s, "r")))
+ return;
+ fseek(disk, 0, SEEK_END);
+ f->s->n = ftell(disk);
+ str_insure(f->s, f->s->n);
+ rewind(disk);
+ fread(f->s->s, f->s->n, 1, disk);
+ fclose(disk);
+}
+
+static void
+file_save(File* f)
+{
+ FILE* disk;
+
+ disk = fopen(f->name->s, "w");
+ fwrite(f->s->s, f->s->n, 1, disk);
+ fclose(disk);
+}
+
+static void
fr_add(Frame* fr, Posn p0, Posn p1)
{
fr_insure(fr, fr->n + 1);
@@ -268,7 +365,7 @@ fr_zero(Frame* fr)
fr->dot = 0;
}
-void
+static void
move(int arg)
{
switch (arg) {
@@ -293,9 +390,89 @@ move(int arg)
};
}
-void
+static void
quit(int arg)
{
win_end();
exit(arg);
}
+
+static void
+str_init(String* p)
+{
+ p->s = emalloc(MINSIZE * sizeof(*p->s));
+ p->n = 0;
+ p->size = MINSIZE;
+}
+
+static void
+str_close(String* p)
+{
+ free(p->s);
+}
+
+static void
+str_zero(String* p)
+{
+ if (p->size > MAXEMPTY) {
+ p->s = erealloc(p->s, MAXEMPTY * sizeof(*p->s));
+ p->size = MAXEMPTY;
+ }
+ p->n = 0;
+}
+
+static void
+str_insure(String* p, ulong n)
+{
+ if (p->size < n) {
+ n += MAXEMPTY;
+ p->s = erealloc(p->s, n * sizeof(*p->s));
+ p->size = n;
+ return;
+ }
+ if (p->size - p->n > MAXEMPTY) {
+ p->size = p->n + MAXEMPTY;
+ p->s = erealloc(p->s, p->size * sizeof(*p->s));
+ }
+}
+
+static void
+str_addc(String* p, int c)
+{
+ str_insure(p, p->n + 1);
+ p->s[p->n++] = c;
+}
+
+static void
+str_adds(String* p, char* s)
+{
+ ulong n;
+
+ n = strlen(s);
+ str_insure(p, p->n + n);
+ memmove(p->s + p->n, s, n);
+ p->n += n;
+}
+
+static void
+str_delc(String* p)
+{
+ if (p->n)
+ p->s[p->n--] = 0;
+}
+
+static void
+str_insert(String* p, String* q, Posn p0)
+{
+ str_insure(p, p->n + q->n);
+ memmove(p->s + p0 + q->n, p->s + p0, p->n - p0);
+ memmove(p->s + p0, q->s, q->n);
+ p->n += q->n;
+}
+
+static void
+str_delete(String* p, Posn p0, Posn p1)
+{
+ memmove(p->s + p0, p->s + p1, p->n - p1);
+ p->n -= p1 - p0;
+}
diff --git a/sim.h b/sim.h
@@ -1,44 +1,11 @@
#define ED "\x1b[2J"
#define CSI "\x1b["
-#define LENGTH(a) (sizeof(a)/sizeof(a[0]))
-#define FILECOUNT 8
-typedef long Posn;
typedef unsigned char uchar;
typedef unsigned int uint;
typedef unsigned long ulong;
typedef unsigned short ushort;
-enum {
- Up,
- Down,
- Left,
- Right
-};
-
-typedef struct {
- Posn p0, p1;
-} Address ;
-
-typedef struct {
- char* s;
- ulong n; /*number of filled characters*/
- ulong size;
-} String;
-
-typedef struct {
- String* s;
- String* name;
- Address dot;
-} File;
-
-typedef struct {
- uchar key;
- void (*func)(int);
- int value;
-} Key;
-
-
typedef struct {
ushort wx, wy;
} Window;
@@ -46,14 +13,6 @@ typedef struct {
void die(char* fmt, ...);
void* emalloc(ulong n);
void* erealloc(void* p, ulong n);
-void file_close(File* f);
-void file_init(File* f);
-void file_load(File* f);
-void file_save(File* f);
-void str_adds(String* p, char* s);
-void str_close(String* s);
-void str_init(String* s);
-void str_insure(String* s, ulong n);
void win_end();
void win_init();
void win_query(Window* w);
diff --git a/string.c b/string.c
@@ -1,87 +0,0 @@
-#include <stdlib.h>
-#include <string.h>
-
-#include "sim.h"
-
-#define MINSIZE 16
-#define MAXEMPTY 256
-
-void
-str_init(String* p)
-{
- p->s = emalloc(MINSIZE * sizeof(*p->s));
- p->n = 0;
- p->size = MINSIZE;
-}
-
-void
-str_close(String* p)
-{
- free(p->s);
-}
-
-void
-str_zero(String* p)
-{
- if (p->size > MAXEMPTY) {
- p->s = erealloc(p->s, MAXEMPTY * sizeof(*p->s));
- p->size = MAXEMPTY;
- }
- p->n = 0;
-}
-
-void
-str_insure(String* p, ulong n)
-{
- if (p->size < n) {
- n += MAXEMPTY;
- p->s = erealloc(p->s, n * sizeof(*p->s));
- p->size = n;
- return;
- }
- if (p->size - p->n > MAXEMPTY) {
- p->size = p->n + MAXEMPTY;
- p->s = erealloc(p->s, p->size * sizeof(*p->s));
- }
-}
-
-void
-str_addc(String* p, int c)
-{
- str_insure(p, p->n + 1);
- p->s[p->n++] = c;
-}
-
-void
-str_adds(String* p, char* s)
-{
- ulong n;
-
- n = strlen(s);
- str_insure(p, p->n + n);
- memmove(p->s + p->n, s, n);
- p->n += n;
-}
-
-void
-str_delc(String* p)
-{
- if (p->n)
- p->s[p->n--] = 0;
-}
-
-void
-str_insert(String* p, String* q, Posn p0)
-{
- str_insure(p, p->n + q->n);
- memmove(p->s + p0 + q->n, p->s + p0, p->n - p0);
- memmove(p->s + p0, q->s, q->n);
- p->n += q->n;
-}
-
-void
-str_delete(String* p, Posn p1, Posn p2)
-{
- memmove(p->s + p1, p->s + p2, p->n - p2);
- p->n -= p2 - p1;
-}