Skip to content

Commit

Permalink
C stubs
Browse files Browse the repository at this point in the history
  • Loading branch information
fogleman committed Feb 24, 2012
1 parent a4bd01c commit a2ec38c
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
3 changes: 3 additions & 0 deletions build.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
gcc -std=c99 -O3 -c -o _engine.o engine.c
gcc -shared -o _engine.dll _engine.o
del _engine.o
64 changes: 64 additions & 0 deletions engine.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include <stdio.h>
#include <stdlib.h>

#define SENTINEL '$'
#define FAILED 0xffffffff

#define DAWG_MORE(x) (x & 0x80000000)
#define DAWG_LETTER(x) ((x >> 24) & 0x7f)
#define DAWG_LINK(x) (x & 0xffffff)

typedef unsigned int DawgRecord;

DawgRecord* dawg;

char* loadFile(char* path) {
FILE* file = fopen(path, "rb");
fseek(file, 0, SEEK_END);
int length = ftell(file);
rewind(file);
char* buffer = (char*)malloc(length);
fread(buffer, 1, length, file);
fclose(file);
return buffer;
}

void init(char* dawgPath) {
dawg = (DawgRecord*)loadFile(dawgPath);
}

void uninit() {
free(dawg);
}

int getDawgRecord(DawgRecord* records, int index, char letter) {
DawgRecord record;
while (1) {
record = records[index];
if (DAWG_LETTER(record) == letter) {
return index;
}
if (!DAWG_MORE(record)) {
return FAILED;
}
index++;
}
}

int checkDawg(DawgRecord* records, char* letters, int length) {
int index = 0;
for (int i = 0; i < length; i++) {
index = getDawgRecord(records, index, letters[i]);
if (index == FAILED) {
return 0;
}
index = DAWG_LINK(records[index]);
}
return 1;
}

int main(int argc, char* argv[]) {
init("files/twl.dawg");
uninit();
return 0;
}
11 changes: 11 additions & 0 deletions engine.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from ctypes import *

dll = None

def init(dll_path, dawg_path):
global dll
dll = CDLL(dll_path)
dll.init(dawg_path)

def uninit():
dll.uninit()

0 comments on commit a2ec38c

Please sign in to comment.