tor_multipath_voip/src/tor_os.c
2019-02-08 14:28:39 +01:00

45 lines
959 B
C

#include "tor_os.h"
void tor_os_create(struct tor_os_str* os, size_t size) {
os->size = size;
os->filled = 0;
os->list = malloc(size * sizeof(char*));
if (os->list == NULL) {
fprintf(stderr, "unable to allocate memory\n");
exit(EXIT_FAILURE);
}
}
char* tor_os_append(struct tor_os_str* os) {
if (os->filled == os->size) {
return NULL;
}
char* cur = os->list[os->filled];
os->filled++;
return cur;
}
void tor_os_read (struct tor_os_str* os, char* file) {
FILE* fd = NULL;
fd = fopen(file, "r");
if (fd == NULL) {
return;
}
size_t len = 0;
int n = 0;
while (1) {
char* dst = tor_os_append(os);
if (dst == NULL) break;
if (getline(&(os->list[n]),&len,fd) == -1) break;
}
fclose(fd);
}
void tor_os_free(struct tor_os_str* os) {
for (int i = 0; i < os->filled; i++) {
free(os->list[i]);
}
free(os->list);
}