diff --git a/header/labels.h b/header/labels.h index e167eb5..5f2295d 100644 --- a/header/labels.h +++ b/header/labels.h @@ -9,5 +9,5 @@ void build_label_table(char label_tokens[][MAX_TOKEN_SIZE], shash_hashtable_t *l void remove_label_definition_tokens(char label_tokens[][MAX_TOKEN_SIZE], dynamic_buffer_t *no_label_definition_tokens); -void replace_labels_with_adresses(char no_label_definition_tokens[][MAX_TOKEN_SIZE], char tokens[][MAX_TOKEN_SIZE], shash_hashtable_t label_table, shash_hashtable_t instruction_information_table); +void replace_labels_with_adresses(char no_label_definition_tokens[][MAX_TOKEN_SIZE], dynamic_buffer_t *tokens, shash_hashtable_t label_table, shash_hashtable_t instruction_information_table); #endif diff --git a/src/labels.c b/src/labels.c index 03924ca..f75c66f 100644 --- a/src/labels.c +++ b/src/labels.c @@ -50,13 +50,18 @@ void remove_label_definition_tokens(char label_tokens[][MAX_TOKEN_SIZE], dynamic no_label_definition_tokens->used += MAX_TOKEN_SIZE; } -void replace_labels_with_adresses(char no_label_definition_tokens[][MAX_TOKEN_SIZE], char tokens[][MAX_TOKEN_SIZE], shash_hashtable_t label_table, shash_hashtable_t instruction_information_table) +void replace_labels_with_adresses(char no_label_definition_tokens[][MAX_TOKEN_SIZE], dynamic_buffer_t *tokens, shash_hashtable_t label_table, shash_hashtable_t instruction_information_table) { // We first copy everything and then modify the tokens array. // This requires a few more cpu cycles but keeps the code simpler for(int i = 0; no_label_definition_tokens[i][0] != EOF; i++) { - strncpy(tokens[i], no_label_definition_tokens[i], MAX_TOKEN_SIZE); + if(tokens->size <= tokens->used) + { + resize_dynamic_buffer(tokens); + } + strncpy(((char (*)[10]) tokens->buffer)[i], no_label_definition_tokens[i], MAX_TOKEN_SIZE); + tokens->used += MAX_TOKEN_SIZE; } int i; @@ -74,7 +79,7 @@ void replace_labels_with_adresses(char no_label_definition_tokens[][MAX_TOKEN_SI // go to label at next token i++; unsigned int *label_addr = shash_get(no_label_definition_tokens[i], strlen(no_label_definition_tokens[i]), &label_table); - sprintf(tokens[i], "%d", *label_addr); + sprintf(((char (*)[10]) tokens->buffer)[i], "%d", *label_addr); // skip ';' i++; @@ -90,5 +95,5 @@ void replace_labels_with_adresses(char no_label_definition_tokens[][MAX_TOKEN_SI i++; } } - tokens[i][0] = EOF; + ((char (*)[10]) tokens->buffer)[i][0] = EOF; } diff --git a/src/main.c b/src/main.c index 854263a..fcdcc32 100755 --- a/src/main.c +++ b/src/main.c @@ -170,7 +170,7 @@ int main(int argc, char **argv) printf("Error: Could not allocate memory for the tokens array\n"); return EXIT_FAILURE; } - replace_labels_with_adresses((char (*)[MAX_TOKEN_SIZE]) no_label_definition_tokens.buffer, (char (*)[MAX_TOKEN_SIZE])tokens.buffer, label_table, instruction_informations); + replace_labels_with_adresses((char (*)[MAX_TOKEN_SIZE]) no_label_definition_tokens.buffer, &tokens, label_table, instruction_informations); printf("Removed labels\n"); free(no_label_definition_tokens.buffer);