blob: a02b99f69107ecfe2ccf90eb12167d4c316a6abb [file] [log] [blame]
/*
* Functions related to storing/retrieving and manipulating DIAL data.
*/
#include "dial_data.h"
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
/**
* Returns the path where data is stored for the given app.
*/
static char* getAppPath(char app_name[]) {
size_t name_size = strlen(app_name) + sizeof(DIAL_DATA_DIR) + 1;
char* filename = (char*) malloc(name_size);
filename[0] = 0;
strncat(filename, DIAL_DATA_DIR, name_size);
strncat(filename, app_name, name_size - sizeof(DIAL_DATA_DIR));
return filename;
}
void store_dial_data(char app_name[], DIALData *data) {
char* filename = getAppPath(app_name);
FILE *f = fopen(filename, "w");
if (f == NULL) {
printf("Cannot open DIAL data output file: %s\n", filename);
exit(1);
}
for (DIALData *first = data; first != NULL; first = first->next) {
fprintf(f, "%s %s\n", first->key, first->value);
}
fclose(f);
}
DIALData *retrieve_dial_data(char *app_name) {
char* filename = getAppPath(app_name);
FILE *f = fopen(filename, "r");
if (f == NULL) {
return NULL; // no dial data found, that's fine
}
DIALData *result = NULL;
char key[256];
char value[256];
while (fscanf(f, "%255s %255s\n", key, value) != EOF) {
DIALData *newNode = (DIALData *) malloc(sizeof(DIALData));
newNode->key = strdup(key);
newNode->value = strdup(value);
newNode->next = result;
result = newNode;
}
fclose(f);
return result;
}