Skip to content

Commit

Permalink
create default config file on first launch #373
Browse files Browse the repository at this point in the history
create a hex dump of example config file with xxd in autogenerate.sh
use this to create a default config file in home dir on first launch
if no file is found.

this only applies if the path to config file is not specified
  • Loading branch information
karlstav committed Mar 16, 2022
1 parent ce28162 commit ff46ea0
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ ltmain.sh
m4/
missing
version
config_file.h
7 changes: 1 addition & 6 deletions autogen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,4 @@ aclocal
autoconf
automake --add-missing

CONFIGDIR=$XDG_CONFIG_HOME/cava

if [ -z "$XDG_CONFIG_HOME" ]; then CONFIGDIR=$HOME/.config/cava; fi

mkdir -p "$CONFIGDIR"
[ -f "$CONFIGDIR"/config ] || cp example_files/config "$CONFIGDIR"/config
xxd -i example_files/config config_file.h # make a hex dump of default config file to be generated in home dir on first launch
6 changes: 6 additions & 0 deletions config.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "config.h"
#include "config_file.h"
#include "util.h"

#include <ctype.h>
Expand Down Expand Up @@ -407,6 +408,11 @@ bool load_config(char configPath[PATH_MAX], struct config_params *p, bool colors
// open file or create file if it does not exist
fp = fopen(configPath, "ab+");
if (fp) {
fseek(fp, 0, SEEK_END);
if (ftell(fp) == 0) {
printf("config file is empty, creating default config file\n");
fwrite(example_files_config, example_files_config_len, sizeof(unsigned char), fp);
}
fclose(fp);
} else {
// try to open file read only
Expand Down

0 comments on commit ff46ea0

Please sign in to comment.