diff options
| author | Martin Owens <doctormo@gmail.com> | 2017-07-01 16:57:37 +0000 |
|---|---|---|
| committer | Martin Owens <doctormo@gmail.com> | 2017-07-01 16:57:37 +0000 |
| commit | 31cc1cb9c2b08f768810396af76071a604fc90aa (patch) | |
| tree | fa9632e1ecbdad0ac39cec8612a9207d929c8f6a /src/io | |
| parent | allows mac os build to fail as we have no mac runners yet (diff) | |
| download | inkscape-31cc1cb9c2b08f768810396af76071a604fc90aa.tar.gz inkscape-31cc1cb9c2b08f768810396af76071a604fc90aa.zip | |
Introduce get_filenames which scans paths for all filenames.
Diffstat (limited to 'src/io')
| -rw-r--r-- | src/io/resource.cpp | 64 | ||||
| -rw-r--r-- | src/io/resource.h | 14 |
2 files changed, 75 insertions, 3 deletions
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 <glibmm/miscutils.h> +#include <glibmm/stringutils.h> +#include <glibmm/fileutils.h> #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<Glib::ustring> get_filenames(Type type, std::vector<const char *> extensions, std::vector<const char *> exclusions) +{ + std::vector<Glib::ustring> 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<Glib::ustring> get_filenames(Domain domain, Type type, std::vector<const char *> extensions, std::vector<const char *> exclusions) +{ + std::vector<Glib::ustring> 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<Glib::ustring> &files, Glib::ustring path, std::vector<const char *> extensions, std::vector<const char *> 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 diff --git a/src/io/resource.h b/src/io/resource.h index 0ea5ab4f0..ed877729c 100644 --- a/src/io/resource.h +++ b/src/io/resource.h @@ -64,6 +64,20 @@ Glib::ustring get_path_ustring(Domain domain, Type type, Glib::ustring get_filename(Type type, char const *filename, char const *locale=NULL); +std::vector<Glib::ustring> get_filenames(Type type, + std::vector<const char *> extensions={}, + std::vector<const char *> exclusions={}); + +std::vector<Glib::ustring> get_filenames(Domain domain, Type type, + std::vector<const char *> extensions={}, + std::vector<const char *> exclusions={}); + +void get_filenames_from_path(std::vector<Glib::ustring> &files, + Glib::ustring path, + std::vector<const char *> extensions={}, + std::vector<const char *> exclusions={}); + + char *profile_path(const char *filename); char *homedir_path(const char *filename); char *log_path(const char *filename); |
