Files
assm-test/src/main.c
T

145 lines
3.7 KiB
C

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "../includes/list.h"
typedef struct {
char *opcode;
unsigned char count;
char** parameters;
} Instruction;
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];
if (argc == 1) {
printf("Input files required.\n");
return -1;
}
// for (int i = 1; i < argc; i++) {
// input_files[i - 1] = args[i];
// print_file(args[i]);
// }
//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(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);
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);
}
void print_file(char* file_path) {
FILE *file;
char* line = NULL;
size_t len = 0;
ssize_t bytes_read;
file = fopen(file_path, "r");
if (!file) {
printf("Failed to open '%s' for reading.\n", file_path);
return;
}
while((bytes_read = getline(&line, &len, file)) != -1) {
List* list = get_strings(line);
if (!list) continue;
if (list->size == 0) continue;
// 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];
// printf("Instruction '%s' with %d params ('%s', '%s')\n", inst->opcode, inst->count, inst->parameters[0], inst->parameters[1]);
// }
// free(inst->parameters);
// free(inst);
PrintList(list);
DestroyList(list);
}
fclose(file);
if (line) free(line);
}
List* get_strings(const char* line) {
if (strlen(line) == 0 ) return NULL;
if (line[0] == ';') return NULL; //If the line starts with a semi-colon its just a comment, so ignore it.
List *list = CreateList();
unsigned long lineLength = strlen(line);
char* currentWord = malloc(lineLength + 1);
int index = 0;
for (int i = 0; i < lineLength; i++) {
if (line[i] == ' ' || line[i] == ',') {
if (index > 0) {
currentWord[index] = '\0';
AddListItem(currentWord, strlen(currentWord) + 1, list);
}
index = 0;
continue;
}
if (line[i] == ';' || i + 1 == lineLength) {
if (index > 0) {
if (line[i] != ';') {
currentWord[index] = line[i];
index++;
}
currentWord[index] = '\0';
AddListItem(currentWord, strlen(currentWord) + 1, list);
}
break;
}
currentWord[index] = line[i];
index++;
}
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]);
}
}