Skip to content
Snippets Groups Projects
Commit 531eb385 authored by Toby Murray's avatar Toby Murray
Browse files

wip getting closer

parent edbfbc30
Branches
No related tags found
No related merge requests found
......@@ -5,13 +5,19 @@
#include "debug.h"
/**
The idea is to eventually implement state dumping by writing a series
of commands to a file <file> that we can then reload next time by doing
cat <file> - | ./passbook
**/
const char INSTRUCTION_PUT[] = "put";
const char INSTRUCTION_REMOVE[] = "rem";
const char INSTRUCTION_REM[] = "rem";
const char INSTRUCTION_GET[] = "get";
const char INSTRUCTION_MPW[] = "mpw";
const char INSTRUCTION_SAVE[] = "save";
typedef struct {
char * username;
......@@ -41,6 +47,10 @@ static const node_t * lookup(const node_t *p, const char *url){
return p; // not found
}
static void node_print(const node_t *p){
printf("URL: %s, Username: %s, Password: %s\n",p->url,p->cred.username,p->cred.password);
}
static node_t *node_new(const char *url, const cred_t cred){
node_t *new = malloc(sizeof(node_t));
assert(new != NULL && "new: malloc failed");
......@@ -176,10 +186,131 @@ unsigned int tokenise(char *str, char * toks[], unsigned int toksLen){
/* two extra chars in each line: the newline '\n' and NUL '\0' */
#define INSTRUCTION_LENGTH (MAX_LINE_LENGTH+2)
/* a global instruction buffer */
char inst[INSTRUCTION_LENGTH];
/* password mapping for each url: initially empty */
node_t * map = NULL;
/* a list of node pointers -- the idea is to use these to implement a node
stack that we can then use to traverse the tree iteratively, avoiding
recursion. We need tree traversal e.g. when saving the passbook to a file */
typedef struct nodeptr_list {
const node_t *p;
struct nodeptr_list *next;
} nodeptr_list_t;
nodeptr_list_t * list_push(nodeptr_list_t *lst, const node_t *p){
nodeptr_list_t *n = malloc(sizeof(nodeptr_list_t));
assert(n != NULL && "push: malloc failed");
n->p = p;
n->next = lst;
return n;
}
void list_destroy(nodeptr_list_t *lst){
while (lst != NULL){
nodeptr_list_t *next = lst->next;
free(lst);
lst = next;
}
}
void list_print(nodeptr_list_t *lst){
printf("\nBegin list print:\n");
while (lst != NULL){
node_print(lst->p);
lst = lst->next;
}
printf("End list print.\n\n");
}
void print_all(const node_t *p){
nodeptr_list_t *lst = NULL;
if (p != NULL){
lst = list_push(lst,p);
while(lst != NULL){
// keep recursing left until we can go no further
while (p->left != NULL){
lst = list_push(lst,p->left);
p = p->left;
}
// pop from the stack to simulate the return
const node_t *q = lst->p;
nodeptr_list_t *temp = lst;
lst = lst->next;
free(temp);
// print the node following the return
node_print(q);
// simulate right recursive call
if (q->right != NULL){
lst = list_push(lst,q->right);
p = q->right;
}
}
}
}
/* returns 0 on successful execution of the instruction in inst */
static int execute(void){
assert(0 && "not implemented yet");
char * toks[4]; /* these are pointers to start of different tokens */
const unsigned int numToks = tokenise(inst,toks,4);
debug_printf("got %u tokens\n",numToks);
if (numToks == 0){
/* blank line */
return 0;
}
if (numToks < 2){
return -1;
}
if (strcmp(toks[0],INSTRUCTION_GET) == 0){
if (numToks != 2){
return -1;
}
debug_printf("Looking up: %s\n",toks[1]);
const node_t *p = lookup(map,toks[1]);
if (p != NULL){
node_print(p);
}else{
printf("Not found.\n");
}
} else if (strcmp(toks[0],INSTRUCTION_REM) == 0){
if (numToks != 2){
return -1;
}
debug_printf("Removing: %s\n",toks[1]);
map = rem(map,toks[1]);
} else if (strcmp(toks[0],INSTRUCTION_PUT) == 0){
if (numToks != 4){
return -1;
}
cred_t cred;
cred.username = toks[2];
cred.password = toks[3];
map = put(map,toks[1],cred);
} else if (strcmp(toks[0],INSTRUCTION_SAVE) == 0){
if (numToks != 2){
return -1;
}
debug_printf("Saving to: %s\n",toks[1]);
print_all(map);
}else{
debug_printf("Unrecognised instruction\n");
return -1;
}
return 0;
}
......@@ -191,6 +322,7 @@ static int run(void){
int instructionCount = 0;
while (1){
memset(inst,0,sizeof(inst));
char * res = fgets(inst,MAX_LINE_LENGTH+2,f);
if (res == NULL){
if (feof(f)){
......@@ -223,8 +355,13 @@ static int run(void){
}
}
instructionCount++;
int r = execute();
if (r != 0){
return r;
}
}
execute();
assert(0 && "Should never reach here");
return 0;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment