Made the List generic, though more care needs to be taken when using it since memory leaks can happen when structs are used with it.
This commit is contained in:
+47
-24
@@ -11,6 +11,7 @@ typedef struct {
|
||||
|
||||
void print_file(char*);
|
||||
List* get_strings(const char*);
|
||||
void PrintList(const List*);
|
||||
|
||||
int main(int argc, char* args[]) {
|
||||
const char *input_files[argc - 1];
|
||||
@@ -25,20 +26,29 @@ int main(int argc, char* args[]) {
|
||||
// print_file(args[i]);
|
||||
// }
|
||||
|
||||
print_file(args[1]);
|
||||
// char str[16];
|
||||
// List *list = CreateList();
|
||||
//print_file(args[1]);
|
||||
struct test {
|
||||
int SomeValue;
|
||||
char* SomeText;
|
||||
};
|
||||
struct test* thing = malloc(sizeof(struct test));
|
||||
thing->SomeText = calloc(1, 16);
|
||||
List *list = CreateList();
|
||||
|
||||
// for (int i = 0; i < 64; i++) {
|
||||
// sprintf(str, "%d", i);
|
||||
// AddListItem(str, list);
|
||||
for (int i = 0; i < 64; i++) {
|
||||
sprintf(thing->SomeText, "%d", i);
|
||||
thing->SomeValue = i;
|
||||
AddListItem(thing, sizeof(struct test), list);
|
||||
|
||||
// if (i % 4 == 0) printf("Size: %d; Capacity: %d\n", list->size, list->capacity);
|
||||
// }
|
||||
if (i % 4 == 0) printf("Size: %d; Capacity: %d\n", list->size, list->capacity);
|
||||
|
||||
// PrintList(list);
|
||||
printf("Some Value: %d Some Text: '%s'\n", ((struct test*) list->root[list->size - 1])->SomeValue, ((struct test*) list->root[list->size - 1])->SomeText);
|
||||
}
|
||||
|
||||
//PrintList(list);
|
||||
free(thing->SomeText);
|
||||
|
||||
// free(list);
|
||||
free(list);
|
||||
}
|
||||
|
||||
void print_file(char* file_path) {
|
||||
@@ -58,23 +68,24 @@ void print_file(char* file_path) {
|
||||
List* list = get_strings(line);
|
||||
|
||||
if (!list) continue;
|
||||
if (list->size == 0) continue;
|
||||
|
||||
Instruction *inst = malloc(sizeof(Instruction));
|
||||
inst->parameters = malloc(sizeof(char*) * 2);
|
||||
// Instruction *inst = malloc(sizeof(Instruction));
|
||||
// inst->parameters = malloc(sizeof(char*) * 2);
|
||||
|
||||
if (strcmp(list->root[0], "mov") == 0) {
|
||||
inst->opcode = "mov";
|
||||
inst->count = 2;
|
||||
inst->parameters[0] = list->root[1];
|
||||
inst->parameters[1] = list->root[2];
|
||||
// if (strcmp(list->root[0], "mov") == 0) {
|
||||
// inst->opcode = "mov";
|
||||
// inst->count = 2;
|
||||
// inst->parameters[0] = list->root[1];
|
||||
// inst->parameters[1] = list->root[2];
|
||||
|
||||
printf("Instruction '%s' with %d params ('%s', '%s')\n", inst->opcode, inst->count, inst->parameters[0], inst->parameters[1]);
|
||||
}
|
||||
// printf("Instruction '%s' with %d params ('%s', '%s')\n", inst->opcode, inst->count, inst->parameters[0], inst->parameters[1]);
|
||||
// }
|
||||
|
||||
free(inst->parameters);
|
||||
free(inst);
|
||||
// free(inst->parameters);
|
||||
// free(inst);
|
||||
|
||||
//PrintList(list);
|
||||
PrintList(list);
|
||||
DestroyList(list);
|
||||
}
|
||||
|
||||
@@ -96,7 +107,7 @@ List* get_strings(const char* line) {
|
||||
if (line[i] == ' ' || line[i] == ',') {
|
||||
if (index > 0) {
|
||||
currentWord[index] = '\0';
|
||||
AddListItem(currentWord, list);
|
||||
AddListItem(currentWord, strlen(currentWord) + 1, list);
|
||||
}
|
||||
index = 0;
|
||||
continue;
|
||||
@@ -109,7 +120,7 @@ List* get_strings(const char* line) {
|
||||
index++;
|
||||
}
|
||||
currentWord[index] = '\0';
|
||||
AddListItem(currentWord, list);
|
||||
AddListItem(currentWord, strlen(currentWord) + 1, list);
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -119,4 +130,16 @@ List* get_strings(const char* line) {
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
void PrintList(const List* list) {
|
||||
if (!list) return;
|
||||
|
||||
const List *current = list;
|
||||
|
||||
printf("Size: %d; Capacity: %d\n", list->size, list->capacity);
|
||||
|
||||
for(int i = 0; i < list->size; i++) {
|
||||
printf("%d: %s\n", i, list->root[i]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user