~pixelherodev/DSM-9

33de1b59f0cf029381ab4ae3d184e537895f7d97 — Noam Preil a day ago f46676c
tmp
4 files changed, 76 insertions(+), 46 deletions(-)

M cli.c
M dsm9.h
M log.c
M sm2.c
M cli.c => cli.c +31 -42
@@ 6,9 6,9 @@

#include "9compat.h"
#include "util.h"
#include "sm2.h"
#include "dsm9.h"

struct sm2_instance instance;
dsm9deck deck;
ulong max_cards, start;
int html, all;
char *path;


@@ 17,10 17,8 @@ int offset;
void
parse_arguments(int argc, char **argv)
{
	int i;
	float dt;
	long count;
	for (i = 1; i < argc; i += 1){
	for(int i = 1; i < argc; i += 1){
		if (strcmp(argv[i], "-a") == 0){
			all = 1;
			continue;


@@ 54,21 52,12 @@ parse_arguments(int argc, char **argv)
			continue;
		}
		delta();
		count = sm2_load(&instance, argv[i], 0);
		dt = delta();
		if(count < 0){
		if(!dsm9load(argv[i], &deck)){
			fprint(1, "Failed to load %s!\n", argv[i]);
			continue;
		}
		fprint(1, "Loaded %lud cards in %fs\n", count, dt);
		delta();
		count = sm2_load(&instance, argv[i], 1);
		dt = delta();
		if(count < 0){
			fprint(1, "Failed to open log file: '%s.log', continuing without...\n", argv[i]);
			continue;
		}
		fprint(1, "Loaded %lud log entries in %fs\n", count, dt);
		fprint(1, "Loaded %ud cards in %fs\n", deck.cardnum, dt);
	}
}



@@ 133,18 122,26 @@ render(char *msg, int html)
	close(p);
}

void
card_prompt(struct sm2_card card)
static void
prompt(u32int index)
{
	char response;
	fprint(1, "Rendering %s (%d)... ", card.id, card.repetitions);
	if(card.field_count < 2){
		sysfatal("Expected 2 fields in card!");
	if(deck.dues[index] > time(NULL))
		return;
	char *name = dsm9cardname(&deck, index);
	char *front = dsm9cardfront(&deck, index);
	char *back = dsm9cardback(&deck, index);
	if(name == NULL || front == NULL || back == NULL){
		free(name);
		free(front);
		free(back);
		return;
	}
	render(card.fields[0], html);
	fprint(1, "Rendering %s (%d)... ", name, deck.reps[index]);
	render(front, html);
	fprint(1, "enter to continue...");
	response = getchar();
	render(card.fields[1], html);
	render(back, html);
	fprint(1, "grade: ");
	response = getchar();
	while (response < '0' || response > '5') {


@@ 157,39 154,31 @@ card_prompt(struct sm2_card card)
		}
		response = getchar();
	}
	sm2_log(&instance, card.id, response - '0', time(nil));
	dsm9log(&deck, index, response - '0', time(nil));
	// discard newline
	response = getchar();

}

void
main(int argc, char **argv)
{
	struct sm2_card *cards;
	float dt;
	default_options();
	u32int count;
	sm2_init(&instance);
	pin(0);
	parse_arguments(argc, argv);
	delta();
	if(all){
		cards = instance.cards;
		count = instance.card_count;
	}else
		cards = h_find_cards(&instance, nsec() / 1000000000 + offset, &count);
	dt = delta();
	fprint(1, "Found %lud due cards in %fs\n", count, dt);
	if(count != 0){	
		delta();
		if(!all)
			repetition_sort(cards, count);
		dt = delta();
		fprint(1, "Repetition-sorted %lud cards in %fs\n", count, dt);
		count = count < max_cards ? count : max_cards;
		for(u32int i = 0; i < deck.cardnum; i += 1)
			prompt(i);
		return;
	}
	count = deck.accel.duecount + deck.accel.newcount;
	if(count > 0){
		if(count > max_cards)
			count = max_cards;
		fprint(1, "Testing %lud cards\n", count);
		for(unsigned long i = start; i < count + start; i += 1)
			card_prompt(cards[i]);
		for(u32int i = start; i < count + start; i += 1)
			prompt(i);
	}
}

M dsm9.h => dsm9.h +2 -2
@@ 49,9 49,9 @@ void dsm9unload(dsm9deck*);
/* Fetches the name of a card, by index */
char *dsm9cardname(dsm9deck*, uint32_t);
/* Fetches the front of a card, by index */
char *dsm9cardfront(uint32_t);
char *dsm9cardfront(dsm9deck*, uint32_t);
/* Fetches the back of a card, by index */
char *dsm9cardback(uint32_t);
char *dsm9cardback(dsm9deck*, uint32_t);
/*
	Appends a log entry to the log file, updates the card information, and flushes the card information to the DSM accelerator file.
*/

M log.c => log.c +0 -1
@@ 10,7 10,6 @@
#endif

#include <stdio.h>
#include "sm2.h"
#include "dsm9.h"
#include "util.h"


M sm2.c => sm2.c +43 -1
@@ 277,7 277,7 @@ dsm9stream(dsm9deck *deck, uint32_t index)
			if(field == 0)
				front = cindex + 3;
			else if(field == 1)
				back = index + 3;
				back = cindex + 3;
			field += 1;
		}
	}


@@ 292,6 292,48 @@ dsm9stream(dsm9deck *deck, uint32_t index)
}

char *
dsm9cardback(dsm9deck *deck, uint32_t index)
{
	/* For this, it'd be more efficient to have id and front stored together - however, this also only uses *eight bytes*, so the effect on the cache is neglible */
	uint32_t start = deck->backs[index];
	uint32_t end = index + 1 != deck->cardnum ? deck->ids[index + 1] : deck->decklen;
	/* If card is invalid, or if the card is new and we cannot stream it, return NULL */
	if(start == (uint32_t)-1 || (start == 0 && !dsm9stream(deck, index)))
		return NULL;
	start = deck->backs[index];
	end = index + 1 != deck->cardnum ? deck->ids[index + 1] : deck->decklen;
	/* Delimiter is three bytes, and we don't want it */
	uint32_t size = end - start - 3;
	char *buf = malloc(size + 1);
	memcpy(buf, deck->deckbuf + start, size);
	buf[size] = 0;
	return buf;
}


char *
dsm9cardfront(dsm9deck *deck, uint32_t index)
{
	/* For this, it'd be more efficient to have id and front stored together - however, this also only uses *eight bytes*, so the effect on the cache is neglible */
	uint32_t start = deck->fronts[index];
	uint32_t end = deck->backs[index];
	/* If card is invalid, or if the card is new and we cannot stream it, return NULL */
	if(end == (uint32_t)-1 || (start == 0 && !dsm9stream(deck, index)))
		return NULL;
	start = deck->fronts[index];
	end = deck->backs[index];
	/* Delimiter is three bytes, and we don't want it */
	printf("start %d end %d\n",start,end);
	if(end <= start)
		return NULL;
	uint32_t size = end - start - 3;
	char *buf = malloc(size + 1);
	memcpy(buf, deck->deckbuf + start, size);
	buf[size] = 0;
	return buf;
}

char *
dsm9cardname(dsm9deck *deck, uint32_t index)
{
	/* For this, it'd be more efficient to have id and front stored together - however, this also only uses *eight bytes*, so the effect on the cache is neglible */