Skip to content

Instantly share code, notes, and snippets.

@jhbruhn
Last active December 20, 2015 03:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jhbruhn/6061555 to your computer and use it in GitHub Desktop.
Save jhbruhn/6061555 to your computer and use it in GitHub Desktop.
POCA!1!!1! build with scons
#include <curses.h>
#include <stdlib.h>
#include "vector.h"
void init(void);
void quit(void);
void redraw_screen(void);
vector buffer;
WINDOW *w;
int main(int argc, char *argv[]) {
init();
int ch;
while((ch = wgetch(w))) {
vector_add(&buffer, ch);
redraw_screen();
}
return(0);
}
void redraw_screen(void) {
int i;
int row;
int col;
for(i = 0; i < vector_count(&buffer); i++) {
int *ch = vector_get(&buffer, i);
if(ch == "\n") {
row++;
col = 0;
} else {
mvaddch(row, col, *ch);
col++;
}
}
refresh();
}
void init(void) {
w = initscr();
atexit(quit);
start_color();
noecho();
clear();
vector_init(&buffer);
}
void quit() {
endwin();
vector_free(&buffer);
}
env = Environment() #init env
poca = env.Program(target = 'poca', source = ['poca.c', 'vector.c'], LIBS=['ncurses', 'form']) #build POCA
# install POCA
env.Install("/usr/local/bin", poca)
env.Alias('install', ['/usr/local/bin'])
#include <stdio.h>
#include <stdlib.h>
#include "vector.h"
void vector_init(vector *v)
{
v->data = NULL;
v->size = 0;
v->count = 0;
}
int vector_count(vector *v)
{
return v->count;
}
void vector_add(vector *v, void *e)
{
if (v->size == 0) {
v->size = 10;
v->data = malloc(sizeof(void*) * v->size);
memset(v->data, '\0', sizeof(void) * v->size);
}
// condition to increase v->data:
// last slot exhausted
if (v->size == v->count) {
v->size *= 2;
v->data = realloc(v->data, sizeof(void*) * v->size);
}
v->data[v->count] = e;
v->count++;
}
void vector_set(vector *v, int index, void *e)
{
if (index >= v->count) {
return;
}
v->data[index] = e;
}
void *vector_get(vector *v, int index)
{
if (index >= v->count) {
return;
}
return v->data[index];
}
void vector_delete(vector *v, int index)
{
if (index >= v->count) {
return;
}
v->data[index] = NULL;
int i, j;
void **newarr = (void**)malloc(sizeof(void*) * v->count * 2);
for (i = 0, j = 0; i < v->count; i++) {
if (v->data[i] != NULL) {
newarr[j] = v->data[i];
j++;
}
}
free(v->data);
v->data = newarr;
v->count--;
}
void vector_free(vector *v)
{
free(v->data);
}
#ifndef VECTOR_H__
#define VECTOR_H__
typedef struct vector_ {
void** data;
int size;
int count;
} vector;
void vector_init(vector*);
int vector_count(vector*);
void vector_add(vector*, void*);
void vector_set(vector*, int, void*);
void *vector_get(vector*, int);
void vector_delete(vector*, int);
void vector_free(vector*);
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment