changed structure
This commit is contained in:
Executable
BIN
Binary file not shown.
Executable
BIN
Binary file not shown.
@@ -0,0 +1,7 @@
|
||||
#ifndef HASH_FNV_H
|
||||
#define HASH_FNV_H
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
uint32_t hasher(void *data, size_t len);
|
||||
@@ -0,0 +1,27 @@
|
||||
#include "stringchicken.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
char myString[100];
|
||||
char ch;
|
||||
FILE *fptr = fopen(argv[1], "r");
|
||||
|
||||
if (fptr == NULL) {
|
||||
printf("file doesn't exist");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
struct stringchicken *mystringarray = create_string_chicken();
|
||||
|
||||
while (fgets(myString, 100, fptr)) {
|
||||
push_SC(myString, mystringarray);
|
||||
}
|
||||
|
||||
print_SC(mystringarray);
|
||||
int mylength = strnlen(myString, 100);
|
||||
|
||||
fclose(fptr);
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
dit is een test
|
||||
test2
|
||||
@@ -0,0 +1,60 @@
|
||||
#include "stringchicken.h"
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
/*
|
||||
parser that can read K=V, maybe also K:V? although would just be configurable seperator
|
||||
I know python uses dictionary which uses a hash table so key -> hash -> value table
|
||||
for this assignment, I could use 2 arrays? though that's... a bit dangerous? or at least tricky, in terms of reassignments.
|
||||
let's just go with a struct with 2 dynamic arrays where the indices are aligned
|
||||
|
||||
ik neem dus eerst 1 die het omzet naar een struct van strings, dan een struct van de KV values.
|
||||
*/
|
||||
|
||||
struct stringchicken *create_string_chicken(){
|
||||
struct stringchicken *mystruct = malloc(sizeof(struct stringchicken));
|
||||
mystruct->length = 0;
|
||||
mystruct->capacity = 2;
|
||||
|
||||
mystruct->data = malloc(sizeof(char*) * mystruct->capacity);
|
||||
|
||||
return mystruct;
|
||||
}
|
||||
|
||||
void push_SC(char *string_input, struct stringchicken *struct_input){
|
||||
if (struct_input->length == struct_input->capacity) {
|
||||
struct_input->capacity*=2;
|
||||
char **tempptr = realloc(struct_input->data, sizeof(char*) * struct_input->capacity);
|
||||
|
||||
if (tempptr == NULL) {
|
||||
exit(1);
|
||||
}
|
||||
|
||||
struct_input->data = tempptr;
|
||||
}
|
||||
|
||||
char *newstring = malloc(strnlen(string_input, 101)+1);
|
||||
strcpy(newstring,string_input);
|
||||
struct_input->data[struct_input->length] = newstring;
|
||||
|
||||
struct_input->length++;
|
||||
}
|
||||
|
||||
void free_SC(struct stringchicken *struct_input){
|
||||
for (int i = 0; i < struct_input->length; i++) {
|
||||
free(struct_input->data[i]);
|
||||
}
|
||||
free(struct_input->data);
|
||||
free(struct_input);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void print_SC(struct stringchicken *input_struct){
|
||||
for (size_t i = 0; i < input_struct->length; i++) {
|
||||
printf("line:%zu equals %s", i, input_struct->data[i]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
#ifndef STRINGCHICKEN_H
|
||||
#define STRINGCHICKEN_H
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
struct stringchicken{
|
||||
char **data;
|
||||
size_t length;
|
||||
size_t capacity;
|
||||
};
|
||||
|
||||
struct stringchicken *create_string_chicken(void);
|
||||
void push_SC(char *string_input, struct stringchicken *struct_input);
|
||||
void print_SC(struct stringchicken *input_struct);
|
||||
|
||||
#endif // STRINGCHICKEN_H
|
||||
|
||||
Reference in New Issue
Block a user