Skip to content

Commit

Permalink
Removed darray & procedure lookup table
Browse files Browse the repository at this point in the history
- Converted procedures & key-bindings to fixed size arrays instead of
  using darray.
- Procedure array also now functions as a lookup table so iteration when
  e.g. invoking a procedure isn't necessary.
  • Loading branch information
br1tney5pear5 committed Oct 25, 2024
1 parent f69c872 commit ee9bec4
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 198 deletions.
1 change: 0 additions & 1 deletion app/sheet.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
#include "sheet/sheet_internal.h"
#include "sheet/buffer.h"
#include "sheet/buffer.c"
#include "sheet/darray.c"
#include "sheet/procedure.c"

/* TODO: move this somewhere else like common or utils */
Expand Down
131 changes: 0 additions & 131 deletions app/sheet/darray.c

This file was deleted.

38 changes: 0 additions & 38 deletions app/sheet/darray.h

This file was deleted.

25 changes: 13 additions & 12 deletions app/sheet/key-bindings.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "procedure.h"
#include "key-bindings.h"
#include "darray.h"

#include <stdio.h>

Expand All @@ -16,8 +15,10 @@
#define keyb_debug(...) ((void)0)
#endif

#define MAX_KEY_BINDINGS 512

static int prev_ch = -1;
static struct darray zsvsheet_key_bindings = {0};
static struct zsvsheet_key_binding key_bindings[MAX_KEY_BINDINGS] = {0};

zsvsheet_handler_status zsvsheet_proc_key_binding_handler(struct zsvsheet_key_binding_context *ctx)
{
Expand All @@ -34,12 +35,14 @@ int zsvsheet_register_key_binding(struct zsvsheet_key_binding *binding)

assert(binding->handler);

/* Lazy init */
if(!darray_alive(&zsvsheet_key_bindings))
darray_init(&zsvsheet_key_bindings, sizeof(struct zsvsheet_key_binding));
darray_push(&zsvsheet_key_bindings, binding);
for(int i = 0; i < MAX_KEY_BINDINGS; ++i) {
if(key_bindings[i].ch == 0) {
key_bindings[i] = *binding;
return 0;
}
}

return 0;
return -ENOMEM;
}

int zsvsheet_register_proc_key_binding(char ch, int proc_id)
Expand All @@ -52,15 +55,13 @@ int zsvsheet_register_proc_key_binding(char ch, int proc_id)

struct zsvsheet_key_binding *zsvsheet_find_key_binding(int ch)
{
darray_for(struct zsvsheet_key_binding, binding, &zsvsheet_key_bindings) {
if(binding->ch == ch)
return binding;
for(int i = 0; i < MAX_KEY_BINDINGS; ++i) {
if(key_bindings[i].ch == ch)
return &key_bindings[i];
}
return NULL;
}



/* Subcommand context, that we later pass to procedures, seems a little out
* of place here. Alternatively this function could only return the binding
* and allow user to execute it.
Expand Down
44 changes: 29 additions & 15 deletions app/sheet/procedure.c
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
#include "procedure.h"
#include "darray.h"
#include <stdint.h>

#if 0
#define proc_debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define proc_debug(...) ((void)0)
#endif

int proc_id_generator = 100; // TODO: shuold be dependant on number of bulitins
struct darray procedures = {};
#define MAX_PROCEDURES 512

/* Each procedure can be given a name and in the future argument specification
* so it can be typed from the procedureline. Simple procedure like call motion
Expand All @@ -20,13 +19,32 @@ struct zsvsheet_procedure {
zsvsheet_proc_handler_fn handler;
};

/* This array both stores procedures and works as a lookup table.
* zero is */
static struct zsvsheet_procedure procedure_lookup[MAX_PROCEDURES] = {0};

static inline bool is_valid_proc_id(zsvsheet_proc_id_t id)
{ return (0 < id && id < MAX_PROCEDURES); }

struct zsvsheet_procedure *zsvsheet_find_procedure(zsvsheet_proc_id_t proc_id)
{
darray_for(struct zsvsheet_procedure, proc, &procedures) {
if(proc->id == proc_id)
return proc;
struct zsvsheet_procedure *proc;
if(!is_valid_proc_id(proc_id))
return NULL;
proc = &procedure_lookup[proc_id];
if(proc->id == ZSVSHEET_PROC_INVALID)
return NULL;
assert(proc->id == proc_id);
return proc;
}

static zsvsheet_proc_id_t zsvsheet_generate_proc_id()
{
for(zsvsheet_proc_id_t id = MAX_PROCEDURES - 1; id > ZSVSHEET_PROC_INVALID; --id) {
if(!is_valid_proc_id(procedure_lookup[id].id))
return id;
}
return NULL;
return ZSVSHEET_PROC_INVALID;
}

zsvsheet_handler_status zsvsheet_proc_invoke(zsvsheet_proc_id_t proc_id, struct zsvsheet_proc_context *ctx)
Expand Down Expand Up @@ -55,15 +73,11 @@ zsvsheet_handler_status zsvsheet_proc_invoke_from_keypress(zsvsheet_proc_id_t pr
static zsvsheet_proc_id_t zsvsheet_do_register_proc(struct zsvsheet_procedure *proc)
{
proc_debug("register proc %d %s\n", proc->id, proc->name ? proc->name : "(unnamed)");
if(!is_valid_proc_id(proc->id))
return -1;
if(zsvsheet_find_procedure(proc->id))
return -1;

/* Lazy init */
if(!darray_alive(&procedures))
darray_init(&procedures, sizeof(struct zsvsheet_procedure));
darray_push(&procedures, proc);

proc_id_generator = proc->id;
procedure_lookup[proc->id] = *proc;
return proc->id;
}

Expand All @@ -78,7 +92,7 @@ zsvsheet_proc_id_t zsvsheet_register_builtin_proc(zsvsheet_proc_id_t id, const c
zsvsheet_proc_id_t zsvsheet_register_proc(const char *name, zsvsheet_proc_handler_fn handler)
{
struct zsvsheet_procedure procedure = {
.id = proc_id_generator + 1, .name = name, .handler = handler
.id = zsvsheet_generate_proc_id(), .name = name, .handler = handler
};
return zsvsheet_do_register_proc(&procedure);
}
8 changes: 7 additions & 1 deletion app/sheet/procedure.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@
#define ZSVSHEET_PROCEDURE_H
#include <stdbool.h>

/* ID's of bulitin procedures, extensions can register more */
/* ID's of bulitin procedures, extensions can register more.
*
* TODO: What specific procedures are bulitin and what are their
* id's is not a concern of the procedure system. This should
* be defined by the registrar who defines those procedures,
* in this case sheet. move it out of here at some point.
*/
enum {
zsvsheet_builtin_proc_unknown = 0,
zsvsheet_builtin_proc_quit,
Expand Down

0 comments on commit ee9bec4

Please sign in to comment.