Added directory scanner

This commit is contained in:
Markus Koch 2017-02-04 14:22:01 +01:00
parent d3b0dce208
commit ac0d8c843d
2 changed files with 32 additions and 4 deletions

View File

@ -13,7 +13,7 @@ int main(int argc, char *argv[])
mainWindow = mainWindow_new(); mainWindow = mainWindow_new();
gtk_widget_show(mainWindow->mainWindow); gtk_widget_show(mainWindow->mainWindow);
mdiary_scan_to_store("/", mainWindow->entryListStore); mdiary_scan_to_store(argv[1], mainWindow->entryListStore);
gtk_main(); gtk_main();

View File

@ -1,21 +1,49 @@
#include "mdiary.h" #include "mdiary.h"
void mdiary_scan_to_store(gchar *base_dir, GtkListStore *entryListStore) static void mdiary_add_file_to_store(gchar *filename, GtkListStore *entryListStore)
{
g_print("Add file: %s\n", filename);
}
static void mdiary_recurse_and_collect(gchar *base_dir, GtkListStore *entryListStore, guint max_level)
{ {
GError *error; GError *error;
GDir *dir = g_dir_open("/", 0, &error); GDir *dir = g_dir_open(base_dir, 0, &error);
gchar *dirname; gchar *dirname;
gchar *fullPath;
GRegex *regex;
GMatchInfo *match_info;
if (!dir) { if (!dir) {
g_print("Error, could not open base directory.\n"); g_print("Error, could not open base directory.\n");
} else { } else {
while (dirname = (gchar *)g_dir_read_name(dir)) { while (dirname = (gchar *)g_dir_read_name(dir)) {
g_print("EL=%s\n", dirname); fullPath = g_strdup_printf("%s/%s", base_dir, dirname);
if (g_file_test(fullPath, G_FILE_TEST_IS_REGULAR)) {
regex = g_regex_new("\\.md$", G_REGEX_CASELESS, 0, NULL);
if (g_regex_match(regex, fullPath, 0, &match_info) &&
g_match_info_matches(match_info))
mdiary_add_file_to_store(fullPath, entryListStore);
g_regex_unref(regex);
} else if (g_file_test(fullPath, G_FILE_TEST_IS_DIR) && max_level) {
mdiary_recurse_and_collect(fullPath, entryListStore, max_level--);
}
g_free(fullPath);
} }
g_dir_close(dir); g_dir_close(dir);
} }
} }
/**
* @brief mdiary_scan_to_store recursively (max. 5 levels) scans the base_dir into the entryListStore.
* @param base_dir The base directory to start scanning in
* @param entryListStore Target GtkListStore
*/
void mdiary_scan_to_store(gchar *base_dir, GtkListStore *entryListStore)
{
mdiary_recurse_and_collect(base_dir, entryListStore, 5);
}
/** /**
* @brief mainWindow_taglist_to_string concatenates a list with commas * @brief mainWindow_taglist_to_string concatenates a list with commas
* @param list is a GList of strings to concatenate * @param list is a GList of strings to concatenate