Created a list structure for strings. Should work for splitting lines of text up just before they're tokenized.
This commit is contained in:
@@ -0,0 +1,20 @@
|
|||||||
|
#ifndef LIST_H
|
||||||
|
#define LIST_H
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#define LISTDEFAULTSIZE 4
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
char** root;
|
||||||
|
int size;
|
||||||
|
int capacity;
|
||||||
|
} List;
|
||||||
|
|
||||||
|
List* CreateList(void);
|
||||||
|
int AddListItem(char *, List *);
|
||||||
|
void PrintList(List*);
|
||||||
|
void DestroyList(List*);
|
||||||
|
|
||||||
|
#endif
|
||||||
+43
@@ -0,0 +1,43 @@
|
|||||||
|
#include "../includes/list.h"
|
||||||
|
|
||||||
|
List* CreateList() {
|
||||||
|
List *new = malloc(sizeof(List));
|
||||||
|
new->root = malloc(sizeof(char*) * LISTDEFAULTSIZE);
|
||||||
|
new->size = 0;
|
||||||
|
new->capacity = LISTDEFAULTSIZE;
|
||||||
|
|
||||||
|
return new;
|
||||||
|
}
|
||||||
|
|
||||||
|
int AddListItem(char *value, List* list) {
|
||||||
|
|
||||||
|
if (list->capacity < list->size + 1) {
|
||||||
|
list->root = realloc(list->root, sizeof(char*) * list->capacity * 2);
|
||||||
|
list->capacity = list->capacity * 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
list->root[list->size] = value;
|
||||||
|
list->size++;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void PrintList(List* list) {
|
||||||
|
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]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void DestroyList(List* list) {
|
||||||
|
|
||||||
|
for(int i = 0; i < list->size; i++) {
|
||||||
|
free(list->root[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
free(list->root);
|
||||||
|
free(list);
|
||||||
|
}
|
||||||
+15
-1
@@ -1,6 +1,7 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include "../includes/list.h"
|
||||||
|
|
||||||
typedef struct opcode {
|
typedef struct opcode {
|
||||||
char op[255];
|
char op[255];
|
||||||
@@ -24,7 +25,20 @@ int main(int argc, char* args[]) {
|
|||||||
// print_file(args[i]);
|
// print_file(args[i]);
|
||||||
// }
|
// }
|
||||||
|
|
||||||
print_file(args[1]);
|
//print_file(args[1]);
|
||||||
|
char str[16];
|
||||||
|
List *list = CreateList();
|
||||||
|
|
||||||
|
for (int i = 0; i < 64; i++) {
|
||||||
|
sprintf(str, "%d", i);
|
||||||
|
AddListItem(str, list);
|
||||||
|
|
||||||
|
if (i % 4 == 0) printf("Size: %d; Capacity: %d\n", list->size, list->capacity);
|
||||||
|
}
|
||||||
|
|
||||||
|
PrintList(list);
|
||||||
|
|
||||||
|
free(list);
|
||||||
}
|
}
|
||||||
|
|
||||||
void print_file(char* file_path) {
|
void print_file(char* file_path) {
|
||||||
|
|||||||
Reference in New Issue
Block a user