Skip to content
This repository has been archived by the owner on Jul 20, 2024. It is now read-only.

Commit

Permalink
Add config getters
Browse files Browse the repository at this point in the history
  • Loading branch information
wvffle committed Jan 8, 2020
1 parent 854b871 commit 3996030
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 11 deletions.
45 changes: 34 additions & 11 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@

// Default config
long config_columns = 4;
long config_wal = 0;
char* config_prompt = "search:";

char *get_str(char *value);
void get_long (char* value, long* res);
void get_str(char *value, char** res);
void get_bool(char* value, long* res);

void open_config() {
char* file = str_concat(get_user_home(), "/.config/waffy/config");
Expand All @@ -33,9 +36,11 @@ void open_config() {
char* value = strtok(NULL, " ");

if (strcmp(key, "columns") == 0) {
config_columns = strtol(value, NULL, 10);
get_long(value, &config_columns);
} else if (strcmp(key, "prompt") == 0) {
config_prompt = get_str(value);
get_str(value, &config_prompt);
} else if (strcmp(key, "wal_enable") == 0) {
get_bool(value, &config_wal);
} else {
fprintf(stderr, "Unknown config option '%s' on line %ld", key, line_number);
}
Expand All @@ -45,20 +50,37 @@ void open_config() {
fclose(fp);
}

char *get_str(char *value) {
if (value[0] != '"') return value;
void get_str(char *value, char** res) {
if (value[0] != '"') {
char* buf = malloc(sizeof(char) * strlen(value));
strcpy(buf, value);
*res = buf;
return;
}

if (str_ends_with(value, "\"")) {
char* res = malloc(sizeof(char) * (strlen(value) - 1));
strcpy(res, value + 1);
res[strlen(res) - 1] = '\0';
return res;
char* buf = malloc(sizeof(char) * (strlen(value) - 2));
value[strlen(value) - 1] = '\0';
strcpy(buf, value + 1);
*res = buf;
return;
}

char* buf = strtok(NULL, " ");
char* res = str_concat(value, str_concat(" ", buf));
get_str(str_concat(value, str_concat(" ", buf)), res);
}

void get_bool(char *value, long *res) {
if (strcmp(value, "1") == 0 || strcmp(value, "yes") == 0 || strcmp(value, "on") == 0 || strcmp(value, "true") == 0) {
*res = 1;
return;
}

*res = 0;
}

return get_str(res);
void get_long(char *value, long *res) {
*res = strtol(value, NULL, 10);
}

void write_config() {
Expand All @@ -70,6 +92,7 @@ void write_config() {
FILE* fp = fopen(file, "w");

fprintf(fp, "columns %ld\n", config_columns);
fprintf(fp, "columns true\n");
fprintf(fp, "prompt \"%s\"\n", config_prompt);

fclose(fp);
Expand Down
1 change: 1 addition & 0 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "utils.h"

long config_columns;
long config_wal;
char* config_prompt;

// Write current config to the file
Expand Down

0 comments on commit 3996030

Please sign in to comment.