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

wip blah

parent 8f3bf07b
Branches
No related tags found
No related merge requests found
......@@ -62,15 +62,15 @@ static node_t *node_new(const char *url, const cred_t cred){
return new;
}
/* updates a node in place */
static void node_edit(node_t * p, const cred_t cred){
/* updates a node's credential in place:
replaces p's credential with that from q and frees q */
static void node_edit_cred(node_t * p, const node_t q){
free(p->cred.username);
free(p->cred.password);
p->cred.username = strdup(cred.username);
assert(p->cred.username != NULL && "edit: strdup username failed");
p->cred.password = strdup(cred.password);
assert(p->cred.password != NULL && "edit: strdup password failed");
p->cred.username = q->username;
p->cred.password = q->password;
free(q);
}
static void node_free(node_t *p){
......@@ -80,6 +80,48 @@ static void node_free(node_t *p){
free(p);
}
/* insert q into p
we assume that if q has children then it cannot already
be present in p. Otherwise, if q has no children and we find its url in p,
then we edit the found entry in place while preserving its children */
static node_t * node_insert(node_t *p, const node_t *q){
if (p == NULL){
return q;
}
/* we store a pointer to a node pointer that remembers where in the
tree the new node needs to be added */
node_t ** new = NULL;
node_t * const start = p;
while (new == NULL) {
int ret = strcmp(p->url,p->url);
if (ret == 0){
assert (q->left == NULL && q->right == NULL && "illegal insertion");
/* edit the node in place */
node_edit_cred(p,q);
/* q is now freed so cannot be used anymore */
return start;
}else if (ret < 0){
if (p->left == NULL){
new = &(p->left);
}else{
p = p->left;
}
}else{
if (p->right == NULL){
new = &(p->right);
}else{
p = p->right;
}
}
}
*new = q;
return start;
}
/* FIXME: UP TO HERE. Now need to make this use node_insert */
/* returns a pointer to the tree with the node added or with the existing
node updated if it was already present */
static node_t * put(node_t *p, const char *url, const cred_t cred){
......@@ -123,14 +165,17 @@ static node_t * rem(node_t *p, const char *url){
while (p != NULL){
int ret = strcmp(url,p->url);
if (ret == 0){
node_t * left = p->left;
node_t * const right = p->right;
left = node_insert(left,right);
node_free(p);
if (pptr != NULL){
*pptr = (node_t *)NULL;
*pptr = left;
return start;
}else{
/* p was the only node in the tree */
assert(p == start);
return NULL;
return left;
}
}else if (ret < 0){
pptr = &(p->left);
......@@ -179,7 +224,7 @@ unsigned int tokenise(char *str, char * toks[], unsigned int toksLen){
}
#define MAX_LINE_LENGTH 1022
#define MAX_INSTRUCTIONS 65536
/* two extra chars in each line: the newline '\n' and NUL '\0' */
#define INSTRUCTION_LENGTH (MAX_LINE_LENGTH+2)
......@@ -436,14 +481,13 @@ static int execute(void){
return 0;
}
static int run(void){
static int run(FILE *f){
debug_printf("Attempting to read program. max line length: %d\n",MAX_LINE_LENGTH);
FILE *f = stdin;
assert(f != NULL);
int instructionCount = 0;
while (1){
while (instructionCount < MAX_INSTRUCTIONS){
memset(inst,0,sizeof(inst));
char * res = fgets(inst,MAX_LINE_LENGTH+2,f);
if (res == NULL){
......@@ -482,6 +526,34 @@ static int run(void){
return r;
}
}
if (feof(f)){
/* final line of file didn't have a trailing newline */
fclose(f);
return instructionCount;
}else{
/* see if we are at end of file by trying to do one more read.
this is necessary if the final line of the file ends in a
newline '\n' character */
char c;
int res = fread(&c,1,1,f);
if (res == 1){
debug_printf("Number of instructions (lines) in file exceeds max (%d)\n",MAX_INSTRUCTIONS);
fclose(f);
return -1;
}else{
if (feof(f)){
/* final read found the EOF, so all good */
fclose(f);
return instructionCount;
}else{
/* probably won't ever get here */
debug_printf("Error while trying to test if line %d was empty\n",instructionCount+1);
fclose(f);
return -1;
}
}
}
assert(0 && "Should never reach here");
return 0;
}
......@@ -503,6 +575,6 @@ int main(void){
p = put(p,"http://blah.com",cred2);
assert(strcmp(lookup(p,"http://blah.com")->cred.username,cred2.username) == 0);
run();
run(stdin);
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment