From 31cc1cb9c2b08f768810396af76071a604fc90aa Mon Sep 17 00:00:00 2001 From: Martin Owens Date: Sat, 1 Jul 2017 18:57:37 +0200 Subject: Introduce get_filenames which scans paths for all filenames. --- src/io/resource.cpp | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 61 insertions(+), 3 deletions(-) (limited to 'src/io/resource.cpp') diff --git a/src/io/resource.cpp b/src/io/resource.cpp index 73dc7117e..dbce8a3f4 100644 --- a/src/io/resource.cpp +++ b/src/io/resource.cpp @@ -20,6 +20,9 @@ #include "config.h" #endif +#include +#include +#include #include "path-prefix.h" #include "io/sys.h" #include "io/resource.h" @@ -57,7 +60,7 @@ gchar *_get_path(Domain domain, Type type, char const *filename) case THEMES: temp = INKSCAPE_THEMEDIR; break; case TUTORIALS: temp = INKSCAPE_TUTORIALSDIR; break; case UIS: temp = INKSCAPE_UIDIR; break; - default: g_assert_not_reached(); + default: temp = ""; } path = g_strdup(temp); } break; @@ -67,7 +70,7 @@ gchar *_get_path(Domain domain, Type type, char const *filename) case GRADIENTS: temp = CREATE_GRADIENTSDIR; break; case PALETTES: temp = CREATE_PALETTESDIR; break; case PATTERNS: temp = CREATE_PATTERNSDIR; break; - default: g_assert_not_reached(); + default: temp = ""; } path = g_strdup(temp); } break; @@ -96,7 +99,7 @@ gchar *_get_path(Domain domain, Type type, char const *filename) } break; } - if (filename) { + if (filename && path) { gchar *temp=g_build_filename(path, filename, NULL); g_free(path); path = temp; @@ -173,6 +176,61 @@ Glib::ustring get_filename(Type type, char const *filename, char const *locale) return result; } +/* + * Get's all the files in a given type, for all domain types. + * + * domain - Optional domain (overload), will check return domains if not. + * type - The type of files, e.g. TEMPLATES + * extentions - A list of extensions to return, e.g. xml, svg + * exclusions - A list of names to exclude e.g. default.xml + */ +std::vector get_filenames(Type type, std::vector extensions, std::vector exclusions) +{ + std::vector ret; + get_filenames_from_path(ret, get_path_ustring(USER, type), extensions, exclusions); + get_filenames_from_path(ret, get_path_ustring(SYSTEM, type), extensions, exclusions); + get_filenames_from_path(ret, get_path_ustring(CREATE, type), extensions, exclusions); + return ret; +} +std::vector get_filenames(Domain domain, Type type, std::vector extensions, std::vector exclusions) +{ + std::vector ret; + get_filenames_from_path(ret, get_path_ustring(domain, type), extensions, exclusions); + return ret; +} + +/* + * Get all the files from a specific path, populating &files vector + * + * &files - Output list to populate + * path - The directory to parse, will add nothing if directory doesn't exist + * extensions - Only add files with these extensions. + * exclusions - Exclude files that exactly match these names. + */ +void get_filenames_from_path(std::vector &files, Glib::ustring path, std::vector extensions, std::vector exclusions) +{ + if(!Glib::file_test(path, Glib::FILE_TEST_IS_DIR)) { + return; + } + + Glib::Dir dir(path); + std::string file = dir.read_name(); + while (!file.empty()){ + bool reject = false; + for (auto &ext: extensions) { + reject = reject || !Glib::str_has_suffix(file, ext); + } + for (auto &exc: exclusions) { + reject = reject || (file == exc); + } + if(!reject) { + files.push_back(Glib::build_filename(path, file)); + } + file = dir.read_name(); + } +} + + /** * Get, or guess, or decide the location where the preferences.xml * file should be located. This also indicates where all other inkscape -- cgit v1.2.3 From f54d8ce2659ae44c10fcdfab5afc9492d39f6dfa Mon Sep 17 00:00:00 2001 From: Martin Owens Date: Sun, 2 Jul 2017 00:10:31 +0200 Subject: Hackfest: Tidy and test for file exists in output. --- src/io/resource.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'src/io/resource.cpp') diff --git a/src/io/resource.cpp b/src/io/resource.cpp index dbce8a3f4..6a388c1ac 100644 --- a/src/io/resource.cpp +++ b/src/io/resource.cpp @@ -23,6 +23,7 @@ #include #include #include + #include "path-prefix.h" #include "io/sys.h" #include "io/resource.h" @@ -218,13 +219,15 @@ void get_filenames_from_path(std::vector &files, Glib::ustring pa while (!file.empty()){ bool reject = false; for (auto &ext: extensions) { - reject = reject || !Glib::str_has_suffix(file, ext); + reject |= !Glib::str_has_suffix(file, ext); } for (auto &exc: exclusions) { - reject = reject || (file == exc); + reject |= Glib::str_has_prefix(file, exc); } + Glib::ustring filename = Glib::build_filename(path, file); + reject |= !Glib::file_test(filename, Glib::FILE_TEST_IS_REGULAR); if(!reject) { - files.push_back(Glib::build_filename(path, file)); + files.push_back(filename); } file = dir.read_name(); } -- cgit v1.2.3