aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlonkaars <l.leblansch@gmail.com>2021-06-02 06:41:47 +0200
committerlonkaars <l.leblansch@gmail.com>2021-06-02 06:41:47 +0200
commit1689146195003828b504f28b8bff8e8af793515d (patch)
treecdeb6add8f37eaf0135ace476d7d87d715bc88d9
parent69024dfb3e73955fd5b03ee3796b40876619ac85 (diff)
loop over lines :tada:
-rw-r--r--musicopy.c98
1 files changed, 63 insertions, 35 deletions
diff --git a/musicopy.c b/musicopy.c
index 95522d8..6eeee89 100644
--- a/musicopy.c
+++ b/musicopy.c
@@ -15,31 +15,35 @@ typedef struct {
char* config_section = "default";
+void print_opts(config_file_opts options) {
+ printf("{\n");
+ printf(" \"music_dir\": \"%s\",\n", options.music_dir);
+ printf(" \"playlist_dir\": \"%s\",\n", options.playlist_dir);
+ printf(" \"existing\": \"%s\",\n", options.existing);
+ printf(" \"include\": \"%s\",\n", options.include);
+ printf(" \"exclude\": \"%s\"\n", options.exclude);
+ printf("}\n");
+}
+
+void append_with_nl(char** dest, char* add) {
+ int len = strlen(*dest) + strlen(add) + 1;
+ char* temp = (char*) malloc(len * sizeof(char));
+ sprintf(temp, "%s%s\n", *dest, add);
+
+ free(*dest);
+ *dest = strdup(temp);
+ free(temp);
+}
+
static int handler(void* user, const char* section, const char* name, const char* value) {
config_file_opts* pconfig = (config_file_opts*)user;
if (strcmp(section, config_section) != 0) return 1;
- if (strcmp(name, "music_dir") == 0) pconfig->music_dir = strdup(value);
- else if (strcmp(name, "playlist_dir") == 0) pconfig->playlist_dir = strdup(value);
- else if (strcmp(name, "existing") == 0) pconfig->existing = strdup(value);
- else if (strcmp(name, "include") == 0) {
- int len = strlen(pconfig->include) + strlen(value) + 1;
- char* include = (char*) malloc(len * sizeof(char));
- sprintf(include, "%s%s\n", pconfig->include, value);
-
- free(pconfig->include);
- pconfig->include = strdup(include);
- free(include);
- }
- else if (strcmp(name, "exclude") == 0) {
- int len = strlen(pconfig->exclude) + strlen(value) + 1;
- char* exclude = (char*) malloc(len * sizeof(char));
- sprintf(exclude, "%s%s\n", pconfig->exclude, value);
-
- free(pconfig->exclude);
- pconfig->exclude = strdup(exclude);
- free(exclude);
- }
+ if (0 == strcmp(name, "music_dir")) pconfig->music_dir = strdup(value);
+ else if (0 == strcmp(name, "playlist_dir")) pconfig->playlist_dir = strdup(value);
+ else if (0 == strcmp(name, "existing")) pconfig->existing = strdup(value);
+ else if (0 == strcmp(name, "include")) append_with_nl(&pconfig->include, strdup(value));
+ else if (0 == strcmp(name, "exclude")) append_with_nl(&pconfig->exclude, strdup(value));
return 1;
}
@@ -49,17 +53,7 @@ void exit_err(char* msg) {
exit(1);
}
-void print_opts(config_file_opts options) {
- printf("{\n");
- printf(" \"music_dir\": \"%s\",\n", options.music_dir);
- printf(" \"playlist_dir\": \"%s\",\n", options.playlist_dir);
- printf(" \"existing\": \"%s\",\n", options.existing);
- printf(" \"include\": \"%s\",\n", options.include);
- printf(" \"exclude\": \"%s\"\n", options.exclude);
- printf("}\n");
-}
-
-int main() {
+config_file_opts load_config() {
char* home = getenv("HOME");
if(!home) exit_err("$HOME could not be read!");
@@ -70,11 +64,45 @@ int main() {
if( access(config_path, F_OK) != 0 ) exit_err("Config file could not be read!");
- config_file_opts config;
- config.exclude = strdup("");
- config.include = strdup("");
+ config_file_opts config = {
+ .exclude = strdup(""),
+ .existing = strdup(""),
+ .include = strdup(""),
+ .music_dir = strdup(""),
+ .playlist_dir = strdup("")
+ };
if (ini_parse(config_path, handler, &config) < 0) exit_err("Can't load configuration file!");
+ return config;
+}
+
+void loop_over_str_nl(char* str, void fn(char*)) {
+ int len = strlen(str);
+ char temp_str[len];
+ int temp_len = 0;
+
+ for(int i = 0; i < len; i++) {
+ if (str[i] != '\n') {
+ temp_len++;
+ strncat(temp_str, &str[i], 1);
+ } else {
+ fn(temp_str);
+
+ temp_len = 0;
+ memcpy(temp_str, "", sizeof(char));
+ }
+ }
+}
+
+void callback(char* line) {
+ printf("line: %s\n", line);
+}
+
+int main() {
+ config_file_opts config = load_config();
+
+ loop_over_str_nl(config.exclude, callback);
+
print_opts(config);
return 0;