Created
October 24, 2011 21:27
-
-
Save sahib/1310366 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <glib.h> | |
#include <stdlib.h> | |
#include <string.h> | |
static void foreach_file(const gchar * song_dir_path, const gchar * regex); | |
static gboolean path_go_up(char * song_dir_path); | |
static void find_in_musictree(const gchar * song_file_path, const gchar * regex, gint up_to); | |
//--------------------------------------------------- | |
// -> stringlib.c | |
gboolean regex_match(const gchar * string, const gchar * regex) | |
{ | |
gboolean retv = FALSE; | |
if(string != NULL) | |
{ | |
if(regex == NULL) | |
return TRUE; | |
retv = g_regex_match_simple(regex, string, G_REGEX_CASELESS, G_REGEX_MATCH_NOTEMPTY); | |
} | |
return retv; | |
} | |
//--------------------------------------------------- | |
// -> utils.c | |
gboolean is_file(const gchar * song_path_dir, const gchar * elem_name) | |
{ | |
gchar * absolute_path = g_strdup_printf("%s%c%s",song_path_dir,G_DIR_SEPARATOR,elem_name); | |
gboolean retv = g_file_test(absolute_path,G_FILE_TEST_IS_REGULAR); | |
g_free(absolute_path); | |
return retv; | |
} | |
//--------------------------------------------------- | |
static void foreach_file(const gchar * song_dir_path, const gchar * regex) | |
{ | |
if(song_dir_path != NULL) | |
{ | |
GError * dir_error = NULL; | |
GDir * song_dir = g_dir_open(song_dir_path,0,&dir_error); | |
if(song_dir != NULL) | |
{ | |
const gchar * entry = NULL; | |
while((entry = g_dir_read_name(song_dir)) != NULL) | |
{ | |
if(regex_match(entry,regex) == TRUE) | |
{ | |
g_printerr("%s\n",entry); | |
} | |
} | |
g_dir_close(song_dir); | |
} | |
else | |
{ | |
g_printerr("Opening %s failed: %s\n",song_dir_path,dir_error->message); | |
g_error_free(dir_error); | |
} | |
} | |
} | |
//--------------------------------------------------- | |
static gboolean path_go_up(char * song_dir_path) | |
{ | |
if(song_dir_path != NULL) | |
{ | |
gchar * sep = strrchr(song_dir_path,G_DIR_SEPARATOR); | |
if(sep != NULL && sep != song_dir_path) | |
{ | |
*sep = 0; | |
return TRUE; | |
} | |
if(sep != NULL && sep == song_dir_path) | |
{ | |
sep[1] = 0; | |
return TRUE; | |
} | |
} | |
return FALSE; | |
} | |
//--------------------------------------------------- | |
static void find_in_musictree(const gchar * song_file_path, const gchar * regex, gint up_to) | |
{ | |
gchar * song_dir_path = g_path_get_dirname(song_file_path); | |
if(song_dir_path != NULL) | |
{ | |
for(gint i = 0; i < up_to; ++i) | |
{ | |
g_printerr("---> Path = %s\n",song_dir_path); | |
foreach_file(song_dir_path,regex); | |
if(path_go_up(song_dir_path) == FALSE) | |
break; | |
} | |
g_free(song_dir_path); | |
} | |
} | |
//--------------------------------------------------- | |
// TODO: escape artist for regex and | |
int main(int argc, char *argv[]) | |
{ | |
gchar * music_dir = "/home/chris/HD/Musik/Apocalyptica/Cult/01 Path"; | |
if(argc > 1) | |
{ | |
music_dir = argv[1]; | |
} | |
find_in_musictree(music_dir,"^(<artist>|artist)\\.(jpg|png|jpeg|gif)$",3); | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment